Robocopy |镜像目标包括源父文件夹

时间:2013-10-22 13:32:34

标签: command-line cmd command-prompt xcopy robocopy

我一直试图整理一个robocopy CMD脚本,以便能够向用户询问路径(复制粘贴或只是手动输入)但我似乎很难过......

这是我目前的代码;

@ECHO OFF
SETLOCAL

:Input
SET /P "source=Please enter or paste the location you want backed up and press ^<Enter^>."
IF "%source%"=="" GOTO Error
GOTO :DoTask
:Error
ECHO You did not specify a location to be backed up! Please try again. & goto :Input

::SET source="":: Obsolete for now, since user input is possible.

:DoTask
REM SET YEAR
set YEAR=%date:~6,4%

REM SET MONTH
set MONTH=%date:~3,2%
if %MONTH% LSS 10 set MONTH=%MONTH:~1,2%
if %MONTH% LSS 10 set MONTH=0%MONTH%

REM SET DAY
set DAY=%date:~0,2%
if %DAY% LSS 10 set DAY=%DAY:~1,2%
if %DAY% LSS 10 set DAY=0%DAY%

REM SET HOUR
set HOUR=%time:~0,2%
if %HOUR% LSS 10 set HOUR=%HOUR:~1,2%
if %HOUR% LSS 10 set HOUR=0%HOUR%

REM SET MINUTE
set MINUTE=%time:~3,2%
if %MINUTE% LSS 10 set MINUTE=%MINUTE:~1,2%
if %MINUTE% LSS 10 set MINUTE=0%MINUTE%

REM SET SECOND
set SECOND=%time:~6,2%
if %SECOND% LSS 10 set SECOND=%SECOND:~1,2%
if %SECOND% LSS 10 set SECOND=0%SECOND%

SET destination="Backups"\%date%
SET logdir="Backups\Logs"\%date%
SET log="Backups\Logs"\%date%\%HOUR%_%MINUTE%_%SECOND%.log

mkdir "%logdir%" 2>NUL

SET copyoptions=/COPYALL /E /ZB /SEC /MIR
:: /COPYALL :: COPY ALL file info.
:: /E       :: Copy Subfolders, including Empty Subfolders.
:: /ZB      :: Use restartable mode; if access denied use Backup mode.
:: /SEC     :: Copy files with SECurity.
:: /MIR     :: MIRror a directory tree.

SET logoptions=/R:0 /W:0 /LOG:%log% /TS /NP /V /ETA /TIMFIX /SECFIX /TEE
:: /R:n:: Number of Retries.
:: /W:n:: Wait time between retries.
:: /LOG:: Output log file.
:: /TS :: Include Source file Time Stamps in the output.
:: /NP :: No Progress - don’t display % copied.
:: /V  :: Produce Verbose output log, showing skipped files.
:: /ETA:: Show Estimated Time of Arrival of copied files.
:: /TIMFIX :: FIX file TIMes on all files, even skipped files.
:: /SECFIX :: FIX file SECurity on all files, even skipped files.
:: /TEE:: Output to console window, as well as the log file.
:: /NFL:: No file logging.
:: /NDL:: No dir logging.

ROBOCOPY %source% %destination% %copyoptions% %logoptions%

:End

如果时间过长,请随时修改此信息,只需链接即可:http://pastebin.com/Np8wBF5b

所以基本上我的问题如下:

如果我尝试输入路径:

C:\Users\Public\Pictures\Sample Pictures

我收到了错误:

ERROR: Invalid Parameter #3 : "Backups\2013/10/22"
Source: C:\Users\Public\Pictures\Sample\
Destination: G:\Pictures

注意错误第一行中的目标如何使用正斜杠而不是反斜杠?但我发现它打破了“Sample”和“Pictures”之间的空间。

如果我使用一个单词,它可以很好地工作,但会让我遇到第二个问题;

Source: C:\Users\User\Pictures\BlackBerry\
Destination: G:\Backups\2013\10\22\

源是正确的,但是在目标文件夹中它不会重新创建“Blackberry”文件夹,只会重新创建其中的内容,这就是我的问题......

为了让用户可以选择多个目的地到XCOPY,但所有会发生的事情是所有文件聚集在一起,没有文件夹结构等......

我的脑子里充满了想要把这一切都弄清楚,所以我真的希望如此善良的灵魂能够帮助我解决这个问题! :)

1 个答案:

答案 0 :(得分:1)

将%source%括在引号中,无论是在何处或在何处设置。

Robocopy "%source%" ...

编辑:进一步评论后的额外代码

更改此行,如下所示:SET destination="Backups"\%date%

for %%a in ("%source%") do SET destination="Backups\%date%\%%~nxa"

此代码的前四行将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec变量。运行下面的批处理文件以查看变量,您也可以修改它们。

@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%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause