无法跟踪批处理中的文件执行流

时间:2018-08-10 06:25:44

标签: windows batch-file

`@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和后续命令不起作用。 如果我排除循环来跟踪文件关闭,则无法在日志文件中获得正确的关闭时间。 请提出建议,如何保持循环并执行后续命令?

0 个答案:

没有答案