【问题标题】:Batch Function Not Copying Properly批处理功能无法正确复制
【发布时间】:2016-02-23 14:16:36
【问题描述】:
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

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

如果我在没有标签的情况下执行所有这些操作。

【问题讨论】:

  • 乍一看,引号可能有问题。试试SET "foldername=svetlana_backup_%newdate%"
  • @npocmaka - 关闭,它在驱动器上创建了文件夹,但返回相同的解析错误

标签: windows batch-file xcopy


【解决方案1】:
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 例程结束时(可能到达文件末尾)控制返回到语句在call 之后 - 这又是例程,这次没有参数,因此是第二行。

试试

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

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

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

也许你还需要

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

用真实的、真正的0 替换当时被抑制的前导零。

【讨论】:

  • 谢谢,成功了!只是出于好奇,您是如何得知=% 的?我一直在阅读和学习批处理文件,但从未遇到过。
  • 从提示中查看set /? - 它在文档中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 2019-12-02
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
相关资源
最近更新 更多