用于根据名称复制文件的批处理脚本 [英] Batch script for copying files based on name

查看:97
本文介绍了用于根据名称复制文件的批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想编写一个执行以下操作的批处理脚本-我有两个文件夹,A和B ... A有10个文件,B有100个文件。我想比较每个文件夹中的文件名和如果B中的名称与A中的名称相同,则将它们复制到文件夹A并覆盖原始文件。

Basically I want to write a little batch script that does the following - I have two folders, A and B... A has 10 files and B has 100. I want to compare the names of the files in each folder and if any in B have the same name as in A, then to copy them to folder A and overwrite the original file.

我试图通过 for命令,但随后我必须在IF后面加上一个IF,以比较我不知道如何正确表达的文件名

I was trying to start off by doing a "for" command on folder A but then I would have to follow that with an IF to compare the filenames which I have no idea how to express correctly

    for /r "C:\folderA" %%a in (*.filetype) do (...)

很抱歉,但是批处理脚本没用。我发现有两个主题涉及相似的问题,但是rly没有足够的答案来提供帮助。

Sry but I am useless with batch scripting. I found a couple of threads covering similar questions, but rly didn't follow the answers enough to help.

这难吗?比较两个文件名的其他线程看起来有点复杂。

Is this difficult? The other threads comparing two filenames looked kinda complicated.

感谢您的帮助:)

推荐答案

尝试一下:

xcopy /s /u /y /i /r /c "C:\folderB\*.filetype" "C:\folderA"




  • / s 复制文件夹和子文件夹

  • / u 仅复制文件预先存在于两个文件夹中

  • / y 禁止覆盖文件的提示

  • / i 告诉xcopy目标是文件夹

  • / r 忽略 READONLY 属性,以防万一(这是可选的)

  • / c 继续复制,即使错误发生(这是可选的)

    • /s copies folders and subfolders
    • /u copies only file that pre-exist in both folders
    • /y suppresses prompts on overwriting files
    • /i tells xcopy that destination is a folder
    • /r ignores READONLY attribute just in case (this is optional)
    • /c continues copying even if errors occur (this is optional)
    • 有关xcopy的更多信息,请参见此处(或 xcopy /?

      More information on xcopy can be found here (or xcopy /?)

      如果这对您不起作用,则类似s应该怎么做:

      If that does not work for you then something like this should do:

      for /r "C:\folderA" %%a in (*.filetype) do if exist "C:\folderB\%%~nxa" copy /y "C:\folderB\%%~nxa" "C:\folderA\%%~nxa"
      

      这是它的作用:


      • 如果存在检查文件是否存在


        • 它检查的路径由文件夹A组成 folderB

        • if exist checks if a file exists
          • the path it checks is composed from folderA path and name and extension of the file found in folderB

          • c $ c> folderB

          • folderA
          • $ b中找到的相似名称的文件$ b
          • from original file found in folderB
          • to similar-named file found in folderA

          %%〜dpnxa 语法如下:


          • %% SOMETHINGa 表示这是一个for循环变量

          • 禁止在文件名两边加上双引号(我们将提供自己的双引号)

          • d 用于磁盘( c:

          • p 用于包含文件夹的路径( \folderA\

          • n 是文件名(即自述文件

          • x 用于扩展(即 .txt

          • %%SOMETHINGa means this is a for-loop variable
          • ~ suppresses double quotes around file names (we will provide our own ones)
          • d is for disk (c:)
          • p is for path to containing folder (\folderA\)
          • n is for name of the file (i.e. readme)
          • x is for extension (i.e. .txt)

          您可以随意混合和匹配它们。更多信息,请此处,或尝试来获取/?

          You can mix and match those as you like. More info is here or try for /?

          如果逻辑需要更复杂,我建议使用 括号+ 延迟扩展 call:label call /?

          If logic needs to be more complicated I suggest either using ( ) brackets + delayed expansion or call :label (call /?)

          更新:更正了 FolderA FolderB

          这篇关于用于根据名称复制文件的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆