1

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

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.

8
  • 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
    Commented Apr 27, 2012 at 20:19
  • 1
    @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
    Commented Apr 28, 2012 at 11:43
  • @barlop True, or I could use do copy /y "%f" c:\YourDestination
    – Mark Allen
    Commented Apr 30, 2012 at 21:36
  • 1
    @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
    Commented May 1, 2012 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
    Commented May 1, 2012 at 1:42

1 Answer 1

5

for /f "tokens=*" %f

For more info: for /?

1
  • 1
    Which version of DOS introduced this syntax?
    – user539484
    Commented Jun 10, 2012 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .