Robocopy kept adding backslash in Windows 7 batch script

被刻印的时光 ゝ 提交于 2019-12-11 07:14:16

问题


I have encounted a extra backslash when trying to copy a single file using in the following batch script on 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\7-Zip\7z" 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

Unfortunately, the robocopy.exe kept appending an extra backslash '\' which resulted in itself not able to find source or destination file as follows:

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

  Started : Wed Dec 27 17:36:49 2017

   Source : c:\Backup\20171227\Backup_2017-12-27_17-36-49.zip\
     Dest : X:\Backup\20171227\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:\Backup\20171227\Backup_2017-12-27_17-36-49.zip'`\
The system cannot find the file specified.

Robocopy appears to try copy source and destination directory as opposed to file.

In short, how to remove the extra backslash that showed up in source and destination? Also, how to make robocopy to only copy file?

I have looked up many similar search results without finding the answer.

Thanks in advance,

George


回答1:


ROBOCOPY should be used here to copy the single file

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

to directory X:\Backup\20171227\ with same name.

ROBOCOPY appends a backslash to source and destination because of ROBOCOPY is designed for copying multiple files and expects that source is a directory and destination is also a directory. Run in a command prompt window robocopy /? and read at least top of output help.

For some unknown reason there are additionally used here the options /S to copy from source all subdirectories excluding empty directories and /E to copy all subdirectories including also empty subdirectories. Using /S and /E makes never sense and ROBOCOPY ignores option /S in this conflicting scenario. Also /PURGE and /MIR do not make sense on copying a single file.

The batch file can be optimized to:

@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%\7-Zip\7z.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 is executed by this batch file with a source directory, a destination directory, the single file to copy and the two additional parameters which make sense on copying a single file.

The destination directory X:\Backup\20171227\ is automatically created by ROBOCOPY and therefore must not be created manually before.

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.

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

Read also the Microsoft article about Using Command Redirection Operators.




回答2:


Thanks for your detail advised and I have got it working with the following code:

@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%\7-Zip\7z.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

Below is the batch output:

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

  Started : Thu Dec 28 11:07:51 2017

   Source : C:\Backup\20171228\
     Dest : X:\Backup\20171228\

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

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

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

      New Dir          1    C:\Backup\20171228\
        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

Cheers, George



来源:https://stackoverflow.com/questions/47987940/robocopy-kept-adding-backslash-in-windows-7-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!