简体   繁体   English

如何在 Windows 中使用 xcopy 复制目录结构

[英]How to replicate a directory structure using xcopy in windows

I have a text file containing a list of folders.My text file looks like this:我有一个包含文件夹列表的文本文件。我的文本文件如下所示:

"D:\old\FOLDER1"  
"D:\old\FOLDER2"  
"D:\old\FOLDER3"  
"D:\old\FOLDER4"  
"D:\old\FOLDER5"

all these folders have subfolders and files under it所有这些文件夹下都有子文件夹和文件

what I want to do is use xcopy to copy FOLDER1 , FOLDER2 , FOLDER3 FOLDER4 and FOLDER5 replicate folders ,replicating the structure of those folders so in output , I want to get我想要做的是使用 xcopy 复制FOLDER1FOLDER2FOLDER3 FOLDER4FOLDER5复制文件夹,复制这些文件夹的结构,因此在输出中,我想得到

D:\output\bkup\FOLDER1\............Including all subfolders and files D:\output\bkup\FOLDER2\............Including all subfolders and files D:\output\bkup\FOLDER3\.......... Including all subfolders and files 
D:\output\bkup\FOLDER4\............Including all subfolders and files 
D:\output\bkup\FOLDER5\............ Including all subfolders and files

I have written below script which works fine for one folder我写了下面的脚本,它适用于一个文件夹

set sourceFolder="D:\old\FOLDER5"
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder%  "D:\output\bkup%destinationFolder%"

but since # of directories to copy is 100+ ,I like to use a for loop or pass the list of directories to copy in a text file , that's what I don't know how to handle it.但是由于要复制的目录数量为 100+,我喜欢使用 for 循环或将目录列表传递到文本文件中,这就是我不知道如何处理的。

please help me ,I'm not expert in batch file writing.请帮助我,我不是批处理文件编写方面的专家。

You can use for /f to read (parse if needed) a textfile line by line.您可以使用 for /f 逐行读取(如果需要解析)文本文件。 Use "delims=" to read the line as a whole.使用“delims=”来阅读整行。 Don't forget to add quotes to prevent having multiple arguments and strip the quotes in the subprocedure不要忘记添加引号以防止有多个参数并在子过程中去掉引号

for /f "delims=" %%a in (yourtextfile.txt) do call :docopy "%%a"
goto :eof

:docopy
set sourceFolder=%~1
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder%  "D:\output\bkup%destinationFolder%" 
goto :eof

try robocopy , it is more powerfull for your needs, available for XP Prof. or newer:尝试robocopy ,它更适合您的需求,适用于 XP Prof. 或更新版本:

set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
robocopy "%sourceFolder%" "%destinationFolder%" /MIR

This makes a complete MIRror at "%destinationFolder%".这会在“%destinationFolder%”处生成一个完整的 MIRror。

If you want to copy folders from a text file with xcopy , use the following code:如果要使用xcopy从文本文件复制文件夹,请使用以下代码:

set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
for /f "usebackqdelims=" %%i in ("My File With Source Folders.txt") do xcopy /seihry "%%i" "D:\output\bkup%destinationFolder%"

Text file with source folders is "My File With Source Folders.txt" .带有源文件夹的文本文件是"My File With Source Folders.txt"

Taking the solution from this Microsoft forum thread:从这个Microsoft 论坛帖子中获取解决方案:

To copy folder structure of another without copying files.复制另一个文件夹结构而不复制文件。

Here "ProjectsX" folder is replaced into "ProjectsY" - within SVN directory这里“ProjectsX”文件夹被替换为“ProjectsY”——在SVN目录中

/E - include empty directories /E - 包括空目录

/XF * - all files are excluded /XF * - 排除所有文件

/XD ".svn" "obj" - specified directories are excluded /XD ".svn" "obj" - 排除指定目录

D:\SVN>robocopy ProjectsX ProjectsY /E /XF * /XD ".svn" "obj"

Thanks to the hint to robocopy in the previous answer by Endoro .感谢Endoro上一个答案中robocopy的提示。 But you don't need mirroring for this really.但是你真的不需要镜像。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM