简体   繁体   English

如何将基于列表(txt)的文件批量复制到具有相同目录结构的另一个文件夹中?

[英]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.

相关问题 如何基于前缀列表(txt)批量复制文件 - How to batch copy files based on a list (txt) of PREFIXES 批处理文件以在目录中搜索文件列表并复制到单个文件夹 - Batch file to search directory for list of files and copy to single folder 如何使用批处理脚本将前15个txt文件从c驱动器中的一个文件夹复制到c驱动器中的另一个文件夹? - How to copy top 15 txt files from one folder in c drive to another folder in c drive using batch script? 如何使用此批处理文件将所有* .txt文件从子文件夹复制到批处理文件文件夹? - How to copy all *.txt files from subfolder to batch file folder using this batch-file? 批处理:如何根据文件名将文件复制到目录 - BATCH: How can I copy files to directory based on file name 如何在批处理文件中将选定的文件从一个文件夹复制到另一个文件夹? - How to copy selected files from one folder to another in Batch file? 批量将文件复制到另一个文件夹 - Batch to copy files to another folder with breaks 批处理从.txt复制文件名列表并生成另一个.txt - batch that copy list of filesnames from a .txt and generate another .txt 如何递归复制目录结构并仅复制底层中的文件(Windows Batch) - How can I recursivly copy a directory structure and only copy the files in the bottom tier (Windows Batch) 如何复制目录结构但只包含某些文件(使用 Windows 批处理文件) - How to copy a directory structure but only include certain files (using windows batch files)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM