问题描述
在Windows 7 pro上的以下批处理脚本中尝试复制单个文件时,我遇到了一个额外的反斜杠:
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
不幸的是,robocopy.exe不断附加一个额外的反斜杠'\',导致其自身无法按以下方式查找源文件或目标文件:
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似乎尝试复制源目录和目标目录而不是文件.
Robocopy appears to try copy source and destination directory as opposed to file.
简而言之,如何删除源和目标中显示的多余反斜杠?另外,如何使robocopy仅复制文件?
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.
预先感谢
乔治
推荐答案
ROBOCOPY 应该在此处复制单个文件
ROBOCOPY should be used here to copy the single file
C:\Backup\20171227\Backup_2017-12-27_17-36-49.zip
到具有相同名称的目录X:\Backup\20171227\
.
to directory X:\Backup\20171227\
with same name.
ROBOCOPY 会在 source 和 destination 后面加上反斜杠,因为 ROBOCOPY 是为复制多个文件而设计的,并且期望 source 是目录,而 destination 也是目录.在命令提示符窗口robocopy /?
中运行并至少阅读输出帮助的顶部.
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.
由于某些未知的原因,此处还使用选项/S
从 source 复制所有子目录(不包括空目录),以及/E
复制所有子目录,包括空子目录.在这种冲突的情况下,使用/S
和和/E
毫无意义,并且 ROBOCOPY 会忽略选项/S
.同样,/PURGE
和/MIR
对于复制单个文件没有意义.
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
此批处理文件使用 source 目录,目的地目录,要复制的单个文件以及两个附加参数来执行
ROBOCOPY 复制单个文件很有意义.
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.
目标目录X:\Backup\20171227\
由 ROBOCOPY 自动创建,因此不能在之前手动创建.
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 /?
echo /?
for /?
md /?
robocopy /?
set /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
还请阅读有关使用命令重定向操作符的Microsoft文章.
Read also the Microsoft article about Using Command Redirection Operators.
这篇关于Robocopy一直在Windows 7批处理脚本中添加反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!