I have a list of folder locations from which I would like to copy all the contained files at these locations. The list of folder locations are stored on a seperate line within a text file (.txt). All the files should be copied to the same location.

How can I complete this exercise using this list of folders in combination with a copy function at the command prompt?


Considering a file with a similar contents containing a list of directories such as:


xcopy inside a for loop will do the job :)

for /f %a in (list.txt) do xcopy /e %a\* c:\destination_folder\

(by copying the whole directory tree (owing to /e) )

You should add an extra % to the variable name if you want to keep and execute it on a separate .bat file.

  • Do the folder locations in list.txt need to separated with blank rows?
    – BobJim
    May 12 '15 at 14:42
  • 2
    The way @Elvin responded, each folder should be on a separate line in the text file, but no extra blanks. If you need to modify this, check out the for /? help command where it mentions delims and tokens. May 12 '15 at 14:48
  • 1
    No, it's not required (I reformatted my post for avoiding the confusion).
    – user373230
    May 12 '15 at 14:57
  • The script works perfectly when there are no spaces in the folder names. Can the code be modified for it to work when folder paths include spaces?
    – BobJim
    May 12 '15 at 16:17
  • @BobJim yes, just use double quotes in the variable value - "%a"
    – user373230
    May 12 '15 at 16:18

I have taken Elvins code and developed it slightly to solve the issue of spaces being included in folder paths and names.

The For Do command now uses a CSV file to source the folder paths from, it was then necessary to include the "delims=," option within the For command.

for /f "delims=," %a in (D:\Batch_Files\Test_Bench\Folders_List.csv) do copy "%a\*" D:\Batch_Files\Test_Bench\Paste_All_The_Files_Here

The comma delimited records enabled the For /f to access the locations containing spaces but only if the token %a\* is wrapped in " ".

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.