I have a text file with a list of filenames


And I want to copy the same file structure in a different location (e:\backup). The resulting structure would look like this


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?

  • Rather than a batch file, you can use many backup/mirror tools out there. Many of them are free and they also offer nice features such as file type matching, file name matching and many more. – tumchaaditya Sep 25 '14 at 20:00
  • 1
    @tumchaaditya In my particular case, a bach is ideal. It's a one-tiime task. – chepe263 Sep 25 '14 at 20:04
@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

|improve this answer|||||

Try this with a batch file:

for /f "delims=" %%a in (TextFile.txt) do call :BeginCopy "%%a"
goto :end


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

|improve this answer|||||
  • WoW! Very Impressive! So much flags! – chepe263 Sep 25 '14 at 19:38
  • what if the input text filename has spaces? – chepe263 Sep 25 '14 at 19:40
  • It works but xcopy ask to create file or directory. Also, filenames are incomplete. Strange... – chepe263 Sep 25 '14 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.