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.

csauve
  • 268
  • 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
  • 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 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
  • 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 May 01 '12 at 00: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 01 '12 at 01:42
  • Oh wow you're right, sorry! :( I blame a lack of coffee. – Mark Allen May 01 '12 at 18:53
  • @phuclv I edited the question to specify CMD rather than DOS – csauve Feb 12 '19 at 18:38

1 Answers1

5

for /f "tokens=*" %f

For more info: for /?

nobody
  • 76