创建一个批处理文件来执行tasklist并将其输出附加到log.txt

创建一个批处理文件来执行tasklist并将其输出附加到log.txt

我正在尝试创建一个批处理文件,每 3 分钟执行一次任务列表并将其记录到 log.txt 中。我对批处理文件非常陌生,想知道我的批处理文件中哪里出了问题。

@echo off
set "CURRENT_DIR=C:\Documents and Settings\Administrator\My Documents\Downloads\"

:checkLog
if exist "%CURRENT_DIR%\log.txt" goto writeLog
echo Log file exists. Appending data.
if not goto createLog

:createLog
echo Creating log.txt
echo   >"%CURRENT_DIR%\log.txt"

:writeLog
tasklist >> "%CURRENT_DIR%\log.txt"
goto loop

:loop
sleep 3 
goto writeLog

答案1

我认为这行还没有完成:

if not goto createLog

我稍微简化了一下,并使用timeoutsleep

@echo off
set "CURRENT_DIR=C:\Documents and Settings\Administrator\My Documents\Downloads\"

if not exist "%CURRENT_DIR%\log.txt" (
echo Creating log.txt
echo   >"%CURRENT_DIR%\log.txt"
) else (
echo Log file exists. Appending data.
)

:writeLog
tasklist >> "%CURRENT_DIR%\log.txt"
timeout /t 3 > nul
goto writeLog

相关内容