[英]How to copy several specified files from a source folder and its subfolders to a destination folder?
I have source folder: c:\\prefix\\bin
我有源文件夹:
c:\\prefix\\bin
I want to copy a set of specified files from source folder and its subfolders. 我想从源文件夹及其子文件夹复制一组指定的文件。
Lets say I want to copy: 可以说我要复制:
bin
... gtsam.dll
... msvcr120.dll
... intel64\
... vc12\
... tbb.dll
To be more clearly for what I want to copy: 为了更清楚地说明我要复制的内容:
.\gtsam.dll .\msvcr120.dll .\intel64\vc12\tbb.dll
There are many files in the source directory and many subdirectories that I don't want to copy. 源目录中有许多文件,许多子目录也不想复制。 And all the specified files I have to copy do not share a wild card.
而且我必须复制的所有指定文件都不共享通配符。 They are not all
*.dll
to copy to c:\\dst
它们不是全部
*.dll
复制到c:\\dst
How I can do it with the most elegant way? 如何以最优雅的方式做到这一点?
Using copy , or xcopy , or robocopy ? 使用copy还是xcopy或robocopy ?
I suggest to create first a simple text file containing line by line the files to copy with relative path. 我建议首先创建一个简单的文本文件,其中包含逐行复制的文件,并使用相对路径进行复制。
Example for FilesList.txt
: 例如
FilesList.txt
:
gtsam.dll
msvcr120.dll
intel64\vc12\tbb.dll
It is up to you in which directory to store this list file with the names of the files to copy. 由您决定要在哪个目录中存储此列表文件以及要复制的文件名。
The code below expects this file in directory C:\\prefix\\bin
. 下面的代码希望该文件位于目录
C:\\prefix\\bin
。
Then create a batch file with following code: 然后使用以下代码创建一个批处理文件:
@echo off
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
%SystemRoot%\System32\xcopy.exe "%%~F" C:\dst\ /C /H /I /K /Q /R /Y >nul
)
popd
If target is specified with a backslash at end as done here and option /I
is used, console application xcopy expects that C:\\dst
is a directory and even creates the entire directory structure to this directory automatically if not already existing. 如果在目标末尾指定了反斜杠(如此处所述),并且使用了选项
/I
,则控制台应用程序xcopy期望C:\\dst
是目录,甚至在该目录不存在时甚至自动创建该目录的整个目录结构。
Or you use this script with command copy and making sure the destination directory exists before copying the files. 或者,您可以将此脚本与命令复制一起使用,并在复制文件之前确保目标目录存在。
@echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
copy /B /Y "%%~F" C:\dst\ >nul
)
popd
Command md creates also entire directory tree on creating a directory with command extensions enabled as by default. 命令md在创建默认情况下启用命令扩展名的目录时也会创建整个目录树。
In both cases the directory C:\\dst
contains after batch file execution: 在这两种情况下,批处理文件执行后,目录
C:\\dst
包含:
The main advantage of using a list file containing the names of the files to copy with relative path is easy updating in future without the need to change the batch code. 使用包含要复制的具有相对路径的文件名的列表文件的主要优点是将来易于更新,而无需更改批处理代码。
But let's say the files should be copied with duplicating the directory structure from source to destination directory resulting in directory C:\\dst
containing after batch file execution: 但是,假设要复制文件,并复制目录结构,从源目录复制到目标目录,从而导致目录
C:\\dst
在执行批处理文件后包含:
In this case the batch code with xcopy could be: 在这种情况下,使用xcopy的批处理代码可能是:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
%SystemRoot%\System32\xcopy.exe "%%~F" "C:\dst\!RelativePath!" /C /H /I /K /Q /R /Y >nul
)
popd
endlocal
And the batch code using copy could be: 使用复制的批处理代码可能是:
@echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
setlocal EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
md "C:\dst\!RelativePath!" 2>nul
copy /B /Y "%%~F" "C:\dst\!RelativePath!" >nul
)
popd
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully. 为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。
copy /?
echo /?
endlocal /?
if /?
for /?
goto /?
md /?
pause /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
Not tested: 未经测试:
@echo off
set "basedir=c:\prefix\bin"
set "destination=c:\destination"
for /r "%basedir%" %%# in (*gtsam.dll *msvcr120.dll *tbb.dll) do (
copy "%%~f#" "%destination%"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.