【发布时间】:2016-10-05 01:43:33
【问题描述】:
我在批处理文件中有以下代码:
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 foldername=%foldername: =%
set destination=R:\Backup
if exist %destination% (
rem do stuff if destination exists.
) else (
call:GenerateErrorLog "%destination% not found."
)
:GenerateErrorLog
echo %~1 >> error_log_%foldername%.txt
goto:eof
它会生成以下文本文件:
R:\Backup not found.
ECHO is off.
为什么打印 ECHO is off,如何防止?
【问题讨论】:
-
在
IF ELSE子句的最后一个括号之后放置一个GOTO :EOF。但是我看不出有任何理由需要调用标签来将其回显到日志文件中。为什么不将 echo 放在ELSE子句中? -
@Squashman - 批处理文件比我在这里发布的要大得多。我把不相关的代码拿出来贴出来。有很多 else 子句会回显错误,所以我决定创建一个函数。
-
所以你明白你的所有函数都应该在你的批处理文件的末尾,并且你应该有一个
GOTO :EOF或EXIT /B作为你所有函数之前的行,以便它退出批处理文件而不尝试再次执行所有功能? -
@Squashman - 当然可以!
标签: batch-file command-line cmd