首页 > 解决方案 > 无法在批处理 scipt 中跟踪文件执行流程

问题描述

`@ECHO OFF

SET logfile="D:\timer.txt"
SET accessfile="Dashboard.txt"
ECHO "***********************************************************" >> %logfile%
ECHO "Start time %DATE% %Time%" >> %logfile%

if exist D:\2018\%accessfile% goto :labela
if not exist D:\2018\%accessfile% goto :labelb


:labela 
(

Start D:\2018\%accessfile% goto :Runningloop 2>&1 && (

echo "*** Date: %DATE:/=-% and Time: %TIME% :: Dashboard notepad file opened successfully ***" >> %logfile% 

) || ( 
        taskkill /F /FI "WindowTitle eq Dashboard - notepad" /T >> %logfile% 2>&1 && (
        Start  Z:\%accessfile% goto :Runningloop >> %logfile% 2>&1 && echo "*** Date: %DATE:/=-% and Time: %TIME% :: Old instance of file Dashboard is deleted and New instance of file opened successfully ****" >> %logfile%

         ) || (  echo "*** Date: %DATE:/=-% and Time: %TIME% :: Error while closing the current instance of file Dashboard hence unable to Reopen the file   ***" >> %logfile% )


)

:Runningloop
(
CLS

set errorlevel=

tasklist /fi "WindowTitle eq Dashboard - notepad" | find /i "notepad.exe"> NUL

if /i %errorlevel% GTR 0 goto ClosureOfFile

GOTO Runningloop

)
:ClosureOfFile
(
ECHO.

ECHO "Dashbiard notepad file is closed."  >> %logfile%
ECHO "Stop time %DATE% %Time%" >> %logfile%
exit

)

) 
exit /b 


:labelb 
(

echo "*** Date: %DATE:/=-% and Time: %TIME% :: Failure, File does not exist , Please Check *****" >> %logfile%
echo "*** Date: %DATE:/=-% and Time: %TIME% :: End of Script" >> %logfile%
echo "**********************************************************************************************************" >> %logfile%

) 

`我正在尝试从批处理脚本打开文件 Dashboard.txt 文件,如果它已经打开,我必须关闭第一个实例并打开新的实例。如果我使用循环从批处理脚本跟踪文件关闭,taskkill 和后续命令不起作用。如果我排除循环来跟踪文件关闭,那么我在日志文件中没有得到正确的关闭时间。请建议,我怎样才能保持循环并执行后续命令?

标签: windowsbatch-file

解决方案


推荐阅读