I need to copy a set of DLL and PDB files from a set of folders recursively into another folder. I don't want to recreate the folder hierarchy in the target folder. I want to use built in Windows tools, e.g. DOS commands.
7 Answers
mkdir targetDir
for /r %x in (*.dll, *pdb) do copy "%x" targetDir\
Use /Y at the end of the above command if you are copying multiple files and don't want to keep answering "Yes".

- 1
- 1

- 33,874
- 33
- 95
- 118
-
nice one. wasn't to sure of the syntax. – Ady Oct 29 '08 at 12:25
-
2Use copy /Y "%x" targetDir\ to clobber any duplicate names. – Peter K. Aug 28 '11 at 20:15
-
1what if you want to specify the source directory without using 'cd'? – TruthOf42 Sep 26 '14 at 13:53
-
1I realise this comes a bit late, but: the FOR command (with /R switch) allows provoding the working directory to traverse: `FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]` – SvenS Oct 15 '15 at 07:33
command XCOPY
example of copying folder recursively:
mkdir DestFolder
xcopy SrcFolder DestFolder /E
(as stated below in the comment following WIKI that command was made available since DOS 3.2)

- 10,722
- 2
- 45
- 46
-
3+1. Note, though, that XCOPY is not just in Windows 7...it's been around forever (according to http://en.wikipedia.org/wiki/XCOPY, since DOS 3.2) – Jeff Olson Jan 15 '13 at 18:00
-
4Might be helpful for someone but doesn't seem to conform with **"I don't want to recreate the folder hierarchy in the target folder."** from OP. – nrodic Jul 11 '15 at 12:23
-
@nrodic, if I understand it correctly this expectation relates to avoiding recreation of source folders hierarchy manually, so the command above copies the SrcFolder with the whole folder structure recursively (and files). – Bronek Aug 17 '15 at 09:10
-
Fair enough, question's text can be interpreted in different ways. But its title provides another piece of information (**"into a single flat folder"**) and accepted answer does just that. From the number of upvotes I would say that your answer has helped a lot of people so I left the comment just for clarification. – nrodic Aug 17 '15 at 16:58
-
@nrodic, I've just analyzed the question carefully (not only title) and you're right, thanks! I guess people who upvoted my answer read the title of this question such superficially as I did and they looked for what I answered. (my answer should be moved to separate question-answer.) – Bronek Aug 17 '15 at 22:18
-
By the number of upvotes you've received I think people looking for your answer must use the same search terms as people looking for the accepted one. It might be helpful though to edit your answer to clarify up front that it will not only copy the files, but the folder structure as well to not confuse people. – Jason Goemaat May 23 '16 at 03:17
-
@JasonGoemaat, thanks for your suggestion but my understanding of the following question requirement: "I don't want to recreate the folder hierarchy in the target folder" is that 'mickdelaney' doesn't want to recreate it manually. He wants hierarchy to be recreated automatically by a windows tool instead. – Bronek May 23 '16 at 13:19
-
I also am trying to do what the base poster asked. So, I've tried both of the first two (highly favored) answers, but neither one works for me. The source files, and some sub-dirs, have spaces in them. Could that be my problem? If so, what is the full solution, in that case? – David Aug 16 '17 at 23:42
Make sure that you have the quotes right if you have spaces in your path.
This is my postbuild event for my TFS build server (that is why there is "%%"). I needed all the test files to be copied over.
if not exist "$(TargetDir)..\SingleFolderOutput" mkdir -p "$(TargetDir)..\SingleFolderOutput"
for /r **%%x** in (*.dll, *.pdb, *.xml, *.xaml, *.exe, *.exe.config) do xcopy **"%%x"** "$(TargetDir)..\SingleFolderOutput" /Y

- 1,496
- 6
- 24
- 29

- 489
- 4
- 13
For gathering PBD files I end up with this:
cmd /q /c for /R "<your_source_folder>" %f in (*.pdb) do xcopy "%f" "<your_destination_folder>" /I /Y

- 375
- 3
- 8
Note: must be two percent chars, if you use for /r from cmd-file:
for /r %%x in (*.dll, *pdb) do copy "%%x" targetDir\

- 1
- 2
@echo off
if %1'==' goto usage
if %2'==' goto usage
if %3'==' goto usage
for /D %%F in (%1\*) do xcopy %%F\%2 %3 /D /Y
for /D %%F in (%1\*.) do call TreeCopy %%F %2 %3
goto end
:usage
@echo Usage: TreeCopy [Source folder] [Search pattern] [Destination folder]
@echo Example: TreeCopy C:\Project\UDI *.xsd C:\Project\UDI\SOA\Deploy\Metadata
:end

- 7
- 1
I'm not aware of any command line tools that do this directly, but you could create a batch script to loop through sub folders, and copy the files you need.
However you may end up with files with duplicate filenames if you place them all in the same folder.

- 4,736
- 2
- 22
- 20