[英]How to batch copy files based on a list (txt) in to another folder with same directory structure?
I have a root directory with over 25,000 files in it.我有一个包含超过 25,000 个文件的根目录。 These files are in loads of different subdirectories.
这些文件位于不同的子目录中。
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory.我还有一个包含 4300 行的文本文件,每一行都是目录中其中一个文件的绝对路径。 Like below,
像下面一样,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.我还有另一个根目录女巫是原始根目录结构的副本,但所有目录都是空的。
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.我想将文本文件中列出的所有文件复制到空目录,并将所有复制的文件放在相关子目录中。
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.如果我使用以下批处理文件,我会不断收到文件覆盖提示,因为它没有将文件复制到正确的目录。
@echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?我该如何修改它以便将文件复制到正确的目录?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure.由于您是从列表中复制特定文件,因此如果您希望它位于类似的文件夹结构中,则需要确保目录结构存在于目标中。 So using the power of the
FOR
command modifiers you can get the file path only from the file name in the file list.因此,使用
FOR
命令修饰符的强大功能,您可以仅从文件列表中的文件名获取文件路径。 You will use that modifier to create the destination directory and also use it as the destination for the XCOPY
command.您将使用该修饰符创建目标目录,并将其用作
XCOPY
命令的目标。
I have taken the liberty of providing best practices for all the code you are using.我冒昧地为您使用的所有代码提供了最佳实践。
@echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.