批处理功能无法正确复制

Batch Function Not Copying Properly

REM Capture the date/time(right down to the second) and then assign it to a variable
set yy=%date:~-4%
set dd=%date:~-7,2%
set mm=%date:~-10,2%
set newdate=%dd%%mm%%yy%_%Time:~0,8%
set newdate=%newdate::=%
SET foldername="svetlana_backup_%newdate%"

SET drive=T: 

REM Source directories
SET documents=%drive%\Documents

REM Destination directories
SET destinationDocuments=%backupDir%\Documents

call:makedirandcopy %documents% %destinationDocuments%

:makedirandcopy
ECHO Making and Copying %~2 Directory
MKDIR %~2
XCOPY %~1 %~2 /E /F

我的桌面上有以下批处理文件,当我运行批处理文件时,它应该在我的目标驱动器上创建一个目录并复制所有文档,但是,它会在我的桌面上创建目录和文档子目录 but,批处理文件所在的位置,并且从不复制文档中的文件。文件名也不正确,它只是将时间分配给目录而不是 date_time.

Passing T:\Documents "T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents
Making and Copying T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents Directory
Making and Copying  Directory
0 File(s) copied 

如果我在没有标签的情况下完成所有这些工作。

call :makedirandcopy %documents% %destinationDocuments%
goto :EOF    

:makedirandcopy
ECHO Making and Copying %~2 Directory
MKDIR "%~2"
XCOPY "%~1" "%~2" /E /F
goto :EOF    

从您的 运行 报告中,您有一行

Passing T:\Documents "T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents

您发布的代码中没有任何内容会生成该行。

然后你有

Making and Copying T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents Directory
Making and Copying  Directory

原因是您正在 calling :makedirandcopy(显示第一行)并且当 called 例程结束时(大概它到达结束- file) 控制返回到 call 之后的语句 - 这又是例程,这次没有参数,因此是第二行。

尝试

call :makedirandcopy "%documents:"=%" "%destinationDocuments:"=%"
goto :EOF    

这将删除每个变量中的多余引号并引用结果。

请注意,由于传递给 :makedirandcopy 的参数中有空格,xcopy 将需要引用这些参数。

也许你还需要

...
set newdate=%newdate::=%
set newdate=%newdate: =0%
...

将时间中被抑制的前导零替换为真实的 0