views:

2453

answers:

6

I have a directory with several subdirectories with files.
How can I copy all files in the subdirectories to a new location?

Edit: I do not want to copy the directories, just the files...

As this is still on XP, I chose the below solution:

 for /D %S IN ("src\*.*") DO  @COPY "%S\" "dest\"

Thanks!

+2  A: 

The Xcopy command should help here.

XCOPY /E SrcDir\*.* DestDir\

Or if you don't want any of the files in SrcDir, just the sub directories, you can use XCOPY in conjunction with the FOR command:

FOR /D %s IN (SrcDir\*) DO @XCOPY /E %s DestDir\%~ns\
Eric Tuttleman
A: 

If you want to keep the same folder structure on the other end, sounds as simple as XCOPY

xcopy c:\old\*.* d:\new\ /s

Use /e instead of /s if you want empty directories copied too.

Cowan
+1  A: 

robocopy "c:\source" "c:\destination" /E

Mark Cidade
RoboCopy is an excellent utility and is very robust. If this is mission critical stuff, then robocopy is likely your main option.
Eric Tuttleman
Also, xcopy is deprecated on Vista in favor of robocopy
Mark Cidade
Ha ha ha, I'm not too worried about xcopy going anywhere anytime soon. Still, good point.
Eric Tuttleman
+1  A: 

If I understood you correctly you have a big directory tree and you want all the files inside it to be in one directory. If that's correct, then I can do it in two lines:

dir /s /b "yourSourceDirectoryTreeHere" > filelist.txt
for /f %f in (filelist.txt) do @copy %f "yourDestinationDirHere"

In a batch file vs. the command line change %f to %%f

Mark Allen
I like your solution, but mine does it in one line, without a temp file. Thanks!
Nescio
Does yours do the entire tree? I didn't think it did but I guess I'll have to try it.
Mark Allen
Eric's answer below looks to be a one line version of my two line command, with no temp file.
Mark Allen
You're right Mark, it does look like I borrowed your idea. I did add the /A-D parameter to the DIR command to get it to not list directories though. Besides that, it's your answer.
Eric Tuttleman
A: 
 for /D %S IN ("src\*.*") DO  @COPY "%S\" "dest\"
Nescio
If that works well for you than great. One issue with it though, is that if you have subdirectories under src with files you want, your command won't get them (as far as I Know at lest). I've added another answer after further understanding your requirements.
Eric Tuttleman
+4  A: 

Ok. With your edit that says you don't want the directory structure, i think you're going to want to use something like this:

for /F "usebackq" %s IN (`DIR /B /S /A-D SrcDir`) DO @(
    XCOPY %s DestDir\%~nxs
)
Eric Tuttleman