如何将特定模式的文件递归复制到 Windows 上的单个平面文件夹中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:38:08  来源:igfitidea点击:

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

windowsfile-iocommand-line

提问by mickdelaney

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.

我需要将一组 DLL 和 PDB 文件从一组文件夹递归复制到另一个文件夹中。我不想在目标文件夹中重新创建文件夹层次结构。我想使用内置的 Windows 工具,例如 DOS 命令。

回答by Alexander Prokofyev

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

如果您要复制多个文件并且不想一直回答“是”,请在上述命令的末尾使用 /Y。

回答by Bronek

command XCOPY

命令 XCOPY

example of copying folder recursively:

递归复制文件夹的示例:

mkdir DestFolder
xcopy SrcFolder DestFolder /E

(as stated below in the comment following WIKIthat command was made available since DOS 3.2)

(如下面在WIKI之后的评论中所述,该命令自 DOS 3.2 起可用)

回答by SteckDEV

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.

这是我的 TFS 构建服务器的构建后事件(这就是为什么有“%%”)。我需要复制所有测试文件。

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

回答by uzrgm

For gathering PBD files I end up with this:

为了收集 PBD 文件,我最终得到了这个:

cmd /q /c for /R "<your_source_folder>" %f in (*.pdb) do xcopy "%f" "<your_destination_folder>" /I /Y

回答by Henrik Thomsen

@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

回答by Ady

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.

但是,如果将它们全部放在同一文件夹中,则最终可能会得到具有重复文件名的文件。