How to copy the structure of files and folders using xcopy from a text file?

I have a text file containing a list of files and folders. I want to use xcopy to replicate what is written in a text file. My text file looks like this:

"C:\FOLDER"  
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"

      

For a given output directory, "C:\OUTPUT"

I would like to replicate the entire structure, so:

"C:\OUTPUT\FOLDER"  
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"

      

How can i do this? So far I have written a for loop that reads on every line of a file, but it copies all files if the line is a folder. I only want to copy and create the files and folders mentioned in the text file.

@echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
   XCOPY /S /E %%a "C:\OUTPUT"
)

      

Am I on the right track?

Thanks and best wishes,

Andrew

+3


source to share


1 answer


Yes, you are close. You just need to use the existing path as the added destination path.

Update

@echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
    md "C:\Output\%%~pA"
    copy /y "%%~fA" "C:\Output\%%~pnxA"
)

      


Original

If %%A

= "C: \ Folder \ Folder2 \ File3.txt", then %%~pA

= Folder \ Folder2



@echo off
for /f "delims=" %%A in (textfile.txt) do (
    md "C:\Output\%%~pA"
    if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)

      

if not exist "%%~fA\*"

make sure to copy the entry only if it is not a directory. See link for more methods and comments

Enter for /?

at the command line to see a list of variable modifiers. %%~A

will remove the surrounding sentences (if any) from the variable.

Post a bug to xcopy and fix # 2 .

An alternative setting, since you most likely won't need the xcopy capabilities.

@echo off
for /f "delims=" %%A in (textfile.txt) do (
    md "C:\Output\%%~pA"
    if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)

      

+5


source







All Articles