How can I recursively copy files of a specific pat

2019-01-12 22:18发布

问题:

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.

回答1:

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



回答2:

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)



回答3:

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


回答4:

@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


回答5:

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.