简体   繁体   中英

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

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.

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. Use "delims=" to read the line as a whole. 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:

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

This makes a complete MIRror at "%destinationFolder%".

If you want to copy folders from a text file with xcopy , use the following code:

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" .

Taking the solution from this Microsoft forum thread:

To copy folder structure of another without copying files.

Here "ProjectsX" folder is replaced into "ProjectsY" - within SVN directory

/E - include empty directories

/XF * - all files are excluded

/XD ".svn" "obj" - specified directories are excluded

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

Thanks to the hint to robocopy in the previous answer by Endoro . But you don't need mirroring for this really.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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