简体   繁体   English

将多个子目录中的文件复制到具有匹配的子目录名称的另一个目录

[英]Copy files in multiple sub-directories to another directory with matching sub-directory names

I can't seem to figure out a good way to do what the title states. 我似乎无法找到一个好的方法来做标题所说的。 I need to move all files found in various sub-directories to another directory with matching sub-directory names. 我需要将在各个子目录中找到的所有文件移动到具有匹配的子目录名称的另一个目录。 The code that I have been working with is: 我一直在使用的代码是:

@echo off
IF EXIST path.txt del /f path.txt
:: Above line for test only

for /f %%i in ('DIR /a:d /b "C:\ProgramData\Test\WTM\Source"') do (
    echo %%i >>path.txt
)
::Pull sub-directory name from working folder

for /f "delims=" %%a in ('DIR /s /a-d /b "C:\ProgramData\Test\WTM\Source"') do (
    for /F %%b in (path.txt) do (
        move /y "%%a" "C:\ProgramData\Test\WTM\Destination\%%b\*.*"
    )
)
PAUSE

What I am trying to do is pull the sub-directory names (as a few may be missing from the destination from time to time), find all of the files to move in the source directory, then move the files using the previously pulled sub-directory names to the destination directory under the respective sub-directory names. 我想要做的是拉出子目录名称(因为一些可能会不时从目标中丢失),找到要在源目录中移动的所有文件,然后使用先前提取的子文件移动文件-directory命名到相应子目录名下的目标目录。 So pulling 所以拉

C:\ProgramData\Test\WTM\Source\123\123.txt,
C:\ProgramData\Test\WTM\Source\456\9681.txt,
C:\ProgramData\Test\WTM\Source\789\1941.txt,
etc...

and moving those files to 并将这些文件移动到

C:\ProgramData\Test\WTM\Destination\123\123.txt,
C:\ProgramData\Test\WTM\Destination\456\9681.txt,
C:\ProgramData\Test\WTM\Destination\789\1941.txt,
etc...

My code never runs correctly and errors vary depending on what I change, but usually I can't get %%b to be passed as the sub-directory name and not %b , or I can get it to work using a parameter (setting set %%i = path and then substituting %%b for %path% ), but then it only copies files to the last sub-directory and not all of them due to how I am utilizing the set command there. 我的代码永远不会正确运行,错误会因我更改而有所不同,但通常我不能将%%b作为子目录名称而不是%b传递,或者我可以使用参数使其工作(设置set %%i = path然后将%%b替换为%path% ),但之后它只将文件复制到最后一个子目录而不是所有这些都归因于我在那里使用set命令。

I need some pointers, I just can't seem to get it to do what I want. 我需要一些指示,我似乎无法让它做我想要的。 Any and all pointers are appreciated! 任何和所有指针都表示赞赏! Thanks a ton! 万分感谢!

I know you have this tagged as batch, but as @FlorianStraub mentioned, you can use robocopy to mirror one directory to another: 我知道你有这个标记为批处理,但正如@FlorianStraub提到的,你可以使用robocopy将一个目录镜像到另一个目录:

robocopy *.* "C:\src" "C:\dest" /E

To break this command down: 要打破此命令:

  • *.* says to copy all files *.*表示要复制所有文件
  • "C:\\src" is your source directory, while "C:\\src"是您的源目录,而
  • "C:\\dest" is your destination directory. "C:\\dest"是您的目标目录。
  • The /E flag at the end says to copy all folders recursively, and include empty folders. 最后的/E标志表示递归复制所有文件夹,并包含空文件夹。

Try running robocopy /? 试试运行robocopy /? from your command line to see more options. 从命令行查看更多选项。

If your dest directory has extra files that you want you get rid of, use the /MIR flag to 1-to-1 mirror your source directory into the destination. 如果您的dest目录包含您想要删除的额外文件,请使用/MIR标志以1对1镜像源目录到目标。 That is, it'll delete any file in the destination that don't also exist within the source. 也就是说,它将删除目标中不存在于源中的任何文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理脚本以查找文件所在的目录,将该目录和子目录中的所有文件复制到另一个目录 - batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory 遍历子目录并在子目录中创建新文件 - Iterate sub directories and create new files in sub-directory 如何使用批处理脚本从当前目录复制文件,而不复制子目录? - How do I copy files from the current directory, but not sub-directories using a batch script? 使用批处理文件通过日志记录将文件复制到子目录中 - Copy the files in sub-directory with logging using batch file 创建/查找批处理脚本,该脚本在目录和子目录中执行“查找和替换”,适用于具有不同名称的 excel 文件 - Create/find batch script which performs a 'find and replace' within a directory and sub-directories, for excel files with varied names 使用批处理脚本将特定文件从源目录复制到目标目录和子目录 - Copy specific file from source directory to target directory and sub-directories using batch script 将子目录中的pdf文件重命名为子目录的名称 - Rename the pdf file inside sub-directories to the name of the sub directory 删除所有子目录和子文件而不删除父/根目录? - Remove All Sub-Directories and Sub-Files Without Removing Parent/Root Directory? 在所有子目录中搜索给定的文件名集*,然后复制到另一个目录 - search given set of file names.* in all sub directories and copy to another directory 在目录和子目录中搜索文件,并编辑这些文件中的特定行 - Searching for files in a directory and sub-directories and editing specific lines within those files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM