Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Similar to How can I copy file recursively ignoring destination directory structure? but in DOS.

I'd like to copy all files recursively, but in the destination folder I'd like to squash the directory structure. I first looked at xcopy, but it does not allow the option to squash the structure (that I can see).

I then looked at for, but I cannot get it to work when there are spaces in the directory structure. For example:

for /F %f in ('dir /b/s/A-D D:\Libs') do @copy "%f" D:\Bin /Y >NUL

This does not copy any files that reside in a path with spaces. This seems to be due to the fact that for splits on spaces as well as carriage return characters.

If there is a solution that works in MSBuild, I would accept that too.

share|improve this question
The 'answer' below is probably right but I'm too lazy to remember all the "tokens=*" parameters of the for command, so instead I'd run dir /b > filelist.txt in the folder containing the files you want copied, then run for /f %f in (filelist.txt) do copy /y %f c:\YourDestination. That does mean that any time there's a name conflict, the last one wins. – Mark Allen Apr 27 '12 at 20:19
@MarkAllen you probably still need for /f "tokens=*" %f or "delims=". Otherwise if there's a space in any item in that file, then it just takes the first "token". Doing "tokens=*" or "delims=" will make it behave sensibly! – barlop Apr 28 '12 at 11:43
@barlop True, or I could use do copy /y "%f" c:\YourDestination – Mark Allen Apr 30 '12 at 21:36
@MarkAllen no, the question had that copy "%f", and it i think is probably needed, but wasn't enough to do the job, because the %f itself only had the part of the directories/paths before the space. – barlop May 1 '12 at 0:59
^ barlop is correct. %f in the copy command only contained the part of the path before the space. "nobody"'s answer below solved this - the %f now contains the full path – csauve May 1 '12 at 1:42
up vote 5 down vote accepted

for /f "tokens=*" %f

For more info: for /?

share|improve this answer
this worked perfectly. thank you! – csauve Apr 27 '12 at 20:21
Which version of DOS introduced this syntax? – user539484 Jun 10 '12 at 1:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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