我有一个批处理文件来创建每周备份。我创建了一个名为mydate的变量,我用它来格式化输出文件夹和回显日志。当我运行批处理并将每一行重定向到日志文件时,它可以正常工作:
set mydate=%date:~10,4%-%date:~4,2%-%date:~7,2%
echo My Date is %mydate% >>TestEchoLog.txt
echo Backup triggered at %mydate% >>TestEchoLog.txt
Cmd窗口:
D:\>set mydate=2017-06-26
D:\>echo My Date is 2017-06-26 1>>TestEchoLog.txt
D:\>echo Backup triggered at 2017-06-26 1>>TestEchoLog.txt
TestEchoLog.txt:
My Date is 2017-06-26
Backup triggered at 2017-06-26
但是,如果我将命令包装到单个重定向中,我的变量就会停止工作:
>>TestEchoLog.txt (
set mydate=%date:~10,4%-%date:~4,2%-%date:~7,2%
echo My Date is %mydate%
echo Backup triggered at %mydate%
Cmd窗口:
D:\>(
set mydate=2017-06-26
echo My Date is
echo Backup triggered at
) 1>>TestEchoLog.txt
TestEchoLog.txt:
My Date is
Backup triggered at
我该如何解决这个问题?显然,一种解决方法是在每一行上都有重定向,但这是一个足够长的指令集,我宁愿不这样做。
谢谢!
答案 0 :(得分:0)
设置变量,然后将其回显到文件中:
set mydate=%date:~10,4%-%date:~4,2%-%date:~7,2%
(
echo My Date is %mydate%
echo Backup triggered at %mydate%
)>>TestEchoLog.txt