2
votes

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.

3
@rene thanks for your time, I get an error 'docopy' is not recognized as an internal or external command,user2434611
made an edit and added support for a text file "My File With Source Folders.txt" and xcopy.Endoro
@user2434611 fixed a bug in my answer by adding a colonrene
@rene thank you very much for helping me out, your code works perfct for my needuser2434611
@rene in practice, my directory name is long, each line in text file is like this "F:\SCRIPTS\00\48\64\06\BAT140_PLM_PRIM_Feature line Proposals table - edit" so after process generated sourceFolder is; "BAT140_PLM_PRIM_Feature" instead of "BAT140_PLM_PRIM_Feature line Proposals table - edit" ,part of the path is chopped off , any ideauser2434611

3 Answers

2
votes

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
2
votes

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

0
votes

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.