Robocopy 在 Windows 7 批处理脚本中不断添加反斜杠

Robocopy kept adding backslash in Windows 7 batch script

我在 Windows 7 pro 上尝试使用以下批处理脚本复制单个文件时遇到了额外的反斜杠:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"


set "DATESTAMP=%YYYY%%MM%%DD%" & set "TIMESTAMP=%HH%%Min%%Sec%"
set "FULLSTAMP=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

set BACKUP_FILE=Backup_%FULLSTAMP%
set LOCAL_BACKUP_PATH=c:\Backup\%DATESTAMP%

set REMOTE_BACKUP_PATH=X:\Backup\%DATESTAMP%

set FULL_LOCAL_BACKUP_FILE=%LOCAL_BACKUP_PATH%\%BACKUP_FILE%.zip

set FULL_REMOTE_BACKUP_FILE=%REMOTE_BACKUP_PATH%\%BACKUP_FILE%.zip

if NOT EXIST "C:\Backup\%DATESTAMP%" ( md C:\Backup\%DATESTAMP% )
"C:\Program Files-Zipz" a -tzip "%LOCAL_BACKUP_PATH%\%BACKUP_FILE%.zip" "C:\Backup\Images.db" "C:\Backup\Images.log" >> C:\Backup\blog.txt 

if NOT EXIST "%REMOTE_BACKUP_PATH%" ( md "%REMOTE_BACKUP_PATH%" )

robocopy %FULL_LOCAL_BACKUP_FILE% %FULL_REMOTE_BACKUP_FILE% /S /E /COPY:DATS /PURGE /MIR /w:1 >> C:\Backup\blog.txt

不幸的是,robocopy.exe 一直附加一个额外的反斜杠“\”,导致它自己无法找到源文件或目标文件,如下所示:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Wed Dec 27 17:36:49 2017

   Source : c:\Backup171227\Backup_2017-12-27_17-36-49.zip\
     Dest : X:\Backup171227\Backup_2017-12-27_17-36-49.zip\

    Files : *.*

  Options : *.* /S /E /COPY:DATS /PURGE /MIR /R:1000000 /W:1 

------------------------------------------------------------------------------

2017/12/27 17:36:49 ERROR 2 (0x00000002) Accessing Source Directory c:\Backup171227\Backup_2017-12-27_17-36-49.zip'`\
The system cannot find the file specified.

Robocopy 似乎尝试复制源目录和目标目录,而不是文件。

简而言之,如何删除源和目标中出现的多余反斜杠?还有,如何让robocopy只复制文件?

我查了很多类似的搜索结果都没有找到答案。

提前致谢,

乔治

这里要用

ROBOCOPY复制单个文件

C:\Backup171227\Backup_2017-12-27_17-36-49.zip

到具有相同名称的目录 X:\Backup171227\

ROBOCOPY 将反斜杠附加到 sourcedestination 因为 ROBOCOPY 是为复制多个文件而设计的,并且期望 source 是一个目录并且 destination 也是一个目录。 运行 在命令提示符下 window robocopy /? 并至少阅读输出帮助的顶部。

由于某些未知原因,这里还使用了选项 /Ssource 复制所有子目录,不包括空目录和 /E 复制所有子目录还包括空子目录。使用 /S /E 毫无意义,并且 ROBOCOPY 在这种冲突的情况下会忽略选项 /S/PURGE/MIR 对复制单个文件没有意义。

批处理文件可以优化为:

@echo off
for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%I"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "DATESTAMP=%YYYY%%MM%%DD%"
set "FULLSTAMP=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

set "BACKUP_FILE=Backup_%FULLSTAMP%.zip"
set "LOCAL_BACKUP_PATH=C:\Backup\%DATESTAMP%"
set "REMOTE_BACKUP_PATH=X:\Backup\%DATESTAMP%"

md "C:\Backup\%DATESTAMP%" 2>nul
"%ProgramFiles%-Zipz.exe" a -tzip "%LOCAL_BACKUP_PATH%\%BACKUP_FILE%" "C:\Backup\Images.db" "C:\Backup\Images.log" >>C:\Backup\blog.txt

%SystemRoot%\System32\robocopy.exe "%LOCAL_BACKUP_PATH%" "%REMOTE_BACKUP_PATH%" "%BACKUP_FILE%" /COPY:DATS /w:1 >>C:\Backup\blog.txt

ROBOCOPY 由这个批处理文件执行,其中有一个 source 目录,一个 destination 目录、要复制的单个文件和两个对复制单个文件有意义的附加参数。

目标目录 X:\Backup171227\ROBOCOPY 自动创建,因此之前不能手动创建。

要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • echo /?
  • for /?
  • md /?
  • robocopy /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

另请阅读有关 Using Command Redirection Operators 的 Microsoft 文章。

感谢您提供的详细信息,我已经使用以下代码进行了处理:

@echo off
for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%I"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "DATESTAMP=%YYYY%%MM%%DD%"
set "FULLSTAMP=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

set "BACKUP_FILE=Backup_%FULLSTAMP%.zip"
set "LOCAL_BACKUP_PATH=C:\Backup\%DATESTAMP%"
set "REMOTE_BACKUP_PATH=X:\Backup\%DATESTAMP%"

md "C:\Backup\%DATESTAMP%" 2>nul
"%ProgramFiles%-Zipz.exe" a -tzip "%LOCAL_BACKUP_PATH%\%BACKUP_FILE%" "C:\Backup\Images.db" "C:\Backup\Images.log" >> C:\Backup\blog.txt

%SystemRoot%\System32\robocopy.exe "%LOCAL_BACKUP_PATH%" "%REMOTE_BACKUP_PATH%" "%BACKUP_FILE%" /COPY:DATS /w:1 >> C:\Backup\blog.txt

下面是批量输出:

ROBOCOPY     ::     Robust File Copy for windows                              
-------------------------------------------------------------------------------

  Started : Thu Dec 28 11:07:51 2017

   Source : C:\Backup171228\
     Dest : X:\Backup171228\

    Files : Backup_2017-12-28_11-07-18.zip

  Options : /COPY:DATS /R:1000000 /W:1 

------------------------------------------------------------------------------

      New Dir          1    C:\Backup171228\
        New File         375.5 m    Backup_2017-12-28_11-07-18.zip   0.0%...............................................................................................................................100%  
------------------------------------------------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         1         0         0         0         0
   Files :         1         1         0         0         0         0
   Bytes :  375.52 m  375.52 m         0         0         0         0
   Times :   0:00:33   0:00:33                       0:00:00   0:00:00
   Speed :            11793404 Bytes/sec.
   Speed :             674.824 MegaBytes/min.
   Ended : Thu Dec 28 11:08:24 2017

干杯, 乔治