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!
-
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\ -
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.
-
robocopy "c:\source" "c:\destination" /EEric Tuttleman : RoboCopy is an excellent utility and is very robust. If this is mission critical stuff, then robocopy is likely your main option.Mark Cidade : Also, xcopy is deprecated on Vista in favor of robocopyEric Tuttleman : Ha ha ha, I'm not too worried about xcopy going anywhere anytime soon. Still, good point. -
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
Nescio : I like your solution, but mine does it in one line, without a temp file. Thanks!Mark Allen : 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.Eric Tuttleman : 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. -
for /D %S IN ("src\*.*") DO @COPY "%S\" "dest\"Eric Tuttleman : 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. -
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 )
0 comments:
Post a Comment