[英]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"
是您的目标目录。 /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.