How to copy files and keep the structure?
I have a text file with a list of filenames
c:\superawesome\lame.mp3
c:\noyoucant\wujuu.mp3
d:\bottle\water.obj
And I want to copy the same file structure in a different location (e:\backup
). The resulting structure would look like this
e:\backup\superawesome\lame.mp3
e:\backup\noyoucant\wujuu.mp3
e:\backup\bottle\water.obj
Backup is an empty folder. I've tried using copy c:\superawesome\lame.mp3 e:\backup\superawesome\lame.mp3
and the same using xcopy
How can I copy the list of files and keep the original folder structure?
Solution 1:
@echo off
setlocal enableextensions disabledelayedexpansion
set "target=e:\backup"
for /f "usebackq delims=" %%a in ("TextFile.txt") do (
md "%target%%%~pa" 2>nul
copy /y "%%a" "%target%%%~pa"
)
For each line (file) inside the list, create, under the target folder, the same path indicated in the readed line (%%~pa
is the path of the element referenced by %%a
). Then, copy the readed file to the target folder
Solution 2:
Try this with a batch file:
for /f "delims=" %%a in (TextFile.txt) do call :BeginCopy "%%a"
goto :end
:BeginCopy
set SrcFolder=%~1
set DestFolder=%SrcFolder:~7,-1%
echo %DestFolder%
xcopy /s /e /i /h /r /y %SrcFolder% "e:\backup\%DestFolder%"
goto :end
For further reading visit: Xcopy