将特定文件复制到另一个文件夹

将特定文件复制到另一个文件夹

您好,我有一个关于使用批处理文件将文件从特定文件夹复制到另一个文件夹的问题。

我有以下代码:

echo off
set arg1=%1
set "arg2=%~2"
set arg3=%3

FOR /R %arg1% %%G IN (%arg3%) DO (
    COPY %%G %arg2%\tempsrc\%%~nxG
)

对于参数的输入:

arg1 = The path where the files are located
arg2 = the output path
arg3 = the file where it should be search for.

例如运行批处理文件:test.bat "C:\Batch\SourceDir" "C:'\Batch\Output" "test.xml"

在 SourceDir 文件夹中,我有两个子文件夹:

  • 1000
    • 文本.xml
  • 2000
    • 测试文件

每个文件夹中test.xml都有该文件。我想将两个文件复制到输出目录,最新的文件应该覆盖前一个文件。

不幸的是,这不起作用。有人能告诉我为什么吗?我收到消息:该系统找不到指定的文件。

答案1

带有空格的文件夹或文件会导致错误,因为它会认为这是多个参数。如果您在 COPY 命令的源和目标之间添加引号,它可能会起作用。

你的脚本将会变成:

echo off
set arg1=%1
set "arg2=%~2"
set arg3=%3

FOR /R %arg1% %%G IN (%arg3%) DO (
    COPY "%%G" "%arg2%\tempsrc\%%~nxG"
)

相关内容