0

我一直在尝试编写一个 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

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

因为如果我用一个词,它就完美了,但又把我带到了我的第二个问题;

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

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

因为用户可以选择 XCOPY 的多个目的地,但所发生的只是所有文件都聚集在一起,没有文件夹结构等......

想弄清楚这一切让我头晕目眩,所以我真的希望如此善良的灵魂能够帮助我解决这个问题!:)

4

1 回答 1

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
于 2013-10-22T16:48:08.400 回答