How can I recursively copy files of a specific pattern into a single flat folder on Windows?

WindowsFile IoCommand Line

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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".

Solution 2 - Windows

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)

Solution 3 - Windows

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

Solution 4 - Windows

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

Solution 5 - Windows

@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

Solution 6 - Windows

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmickdelaneyView Question on Stackoverflow
Solution 1 - WindowsAlexander ProkofyevView Answer on Stackoverflow
Solution 2 - WindowsBronekView Answer on Stackoverflow
Solution 3 - WindowsSteckDEVView Answer on Stackoverflow
Solution 4 - WindowsuzrgmView Answer on Stackoverflow
Solution 5 - WindowsHenrik ThomsenView Answer on Stackoverflow
Solution 6 - WindowsAdyView Answer on Stackoverflow