我有一个运行24/7的批处理文件,以检查某个Programm是否正在运行,如果没有,则启动它。它每10秒钟检查一次,然后这样写一个控制台条目。
@echo off
for /L %%n in (1,0,10) do (
tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i
"AutoPrinter.exe" >nul && (
echo %date% %time:~0,8% AutoPrinter already running
echo %date% %time:~0,8% AutoPrinter already running>>
autostarterlog.txt
) || (
start AutoPrinter.exe
echo %date% %time:~0,8% AutoPrinter started
echo %date% %time:~0,8% AutoPrinter started >> autostarterlog.txt
)
timeout /T 10
)
但是时间戳没有更新,因此输出看起来像这样
20.06.2019 10:45:41 AutoPrinter started
20.06.2019 10:45:41 AutoPrinter already running
20.06.2019 10:45:41 AutoPrinter already running
etc...
反正还有时间更新时间戳,以便它们显示实际时间而不总是显示开始时间吗?
答案 0 :(得分:0)
@echo off
cls
:start
tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i
"AutoPrinter.exe" >nul && (
echo %date% %time:~0,8% AutoPrinter already running
autostarterlog.txt (
echo %date% %time:~0,8% AutoPrinter already running
)
) || (
start AutoPrinter.exe
echo %date% %time:~0,8% AutoPrinter started
autostarterlog.txt (
echo %date% %time:~0,8% AutoPrinter started
)
)
timeout /T 10
goto start
尝试使用goto而不是循环。上面的例子
答案 1 :(得分:0)
查看https://stackoverflow.com/a/13809834/11565682后实际上找到了答案 在启用延迟扩展并将%更改为!之后这是针对具有相同问题的未来人们的工作准则
@echo off
setlocal ENableDelayedExpansion
for /L %%n in (1,0,10) do (
tasklist /nh /fi "imagename eq AutoPrinter.exe" | find /i "AutoPrinter.exe" >nul
&& (
echo !date! !time:~0,8! AutoPrinter laeuft bereits
echo !date! !time:~0,8! AutoPrinter laeuft bereits >> autostarterlog.txt
) || (
start AutoPrinter.exe
echo !date! !time:~0,8! AutoPrinter gestartet
echo !date! !time:~0,8! AutoPrinter gestartet >> autostarterlog.txt
)
timeout /T 10
)