简体   繁体   English

如何将几个指定的文件从源文件夹及其子文件夹复制到目标文件夹?

[英]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还是xcopyrobocopy

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包含:

  • gtsam.dll gtsam.dll
  • msvcr120.dll msvcr120.dll
  • tbb.dll tbb.dll

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在执行批处理文件后包含:

  • gtsam.dll gtsam.dll
  • msvcr120.dll msvcr120.dll
  • intel64 Intel64位
    • vc12 VC12
      • tbb.dll tbb.dll

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.

相关问题 DropIt如何从文件夹和子文件夹复制pdf文件 - DropIt how to copy pdf files from folder and subfolders 将文本文件中具有字符串结构的多个子文件夹中与字符串匹配的文件复制到另一个目标文件夹 - Copy files matching string(s) in a text file out of many subfolders with folder structure to another destination folder Windows批处理将文件从子文件夹复制到一个文件夹 - Windows batch copy files from subfolders to one folder 如何使用Windows命令将文件夹结构和文件复制到目标位置? - How to copy the folder structure and files into the destination using a Windows command? 我坚持使用 powershell 脚本,该脚本必须将多个子文件夹中的所有文件压缩到目标文件夹 - Im stuck on a powershell script that has to compress all files from multiple subfolders to a destination folder 如何将x天的旧文件从文件夹和子文件夹复制到新位置,同时保持Windows中当前的文件夹结构? - how to copy x days old files from folders and subfolders to new location while maintaining current folder structure in windows? 如何从批处理文件中的特定文件夹及其子文件夹中查找名称不带下划线的所有jpeg文件,然后重命名 - How to find all jpeg files having name without underscore from a specific folder and its subfolders in batch file then rename it 重命名文件夹及其子文件夹中的多个文件为固定模式 - Rename multiple files in a folder and its subfolders to a fixed pattern Windows中该文件夹及其子文件夹内的所有文件的列表 - List of all files inside the folder and its subfolders in Windows 将文件复制到文件夹的所有子文件夹 - Copy a file to all subfolders of a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM