简体   繁体   中英

batch script still holding the file after execution

hc.bat

set num=0
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1-3 delims=;" %%a in (server.conf) do (
    findstr /b "^#" %%a >nul 2>&1
    if errorlevel 1 start /B check.bat %%a %%b %num% > check!num!.log
    set /A num=num+1


    )

)
endlocal
exit /b 2

check.bat

set svr=%1
set log=%2
set num=%3
echo %svr% %log% !num!
setlocal ENABLEDELAYEDEXPANSION
copy /y NUL output!num!.tmp >NUL
powershell -command "& {Get-Content %log% | Select-Object -last 1}">> output!num!.tmp
type output!num!.tmp | findstr /m "STUCK" >nul 2>&1 
if %errorlevel%==0 (goto action)
type output!num!.tmp | findstr /m "shut" >nul 2>&1 
if %errorlevel%==0 (goto action)

:action
type output!num!.tmp
del output!num!.tmp
exit /b 2

Two problems with the script 1) the process keeps holding to the log file check0.log 2) In the hc.bat, I'm trying to use the findstr to ignore lines that start with #, but it doesn't seem to be working

hc.bat :

Expand num like !num! (delayed expansion).
I modified the set /A statement to avoid (immediate) expansion right to = .
The redirection > means to overwrite data. Hence I changed it to >> in order to append data.
Insert cmd /C into start /B argument.
At findstr , the /b switch and ^ being the first char. in the search string are redudant. I gave the /r switch to force regular expression mode.
The 3rd for /F token was not used, so I simply removed it.

set num=0
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1,2 delims=;" %%a in (server.conf) do (
    findstr /r "^#" %%a >nul 2>&1
    if errorlevel 1 start /B cmd /C "check.bat %%a %%b !num! >> check!num!.log"
    set /A num=+1
    )
)
endlocal
exit /b 2

check.bat :

Actually you do not need delayed expansion here (although it does no harm).

set svr=%1
set log=%2
set num=%3
echo %svr% %log% %num%
copy /y NUL output%num%.tmp >NUL
powershell -command "& {Get-Content %log% | Select-Object -last 1}">> output%num%.tmp
type output%num%.tmp | findstr /m "STUCK" >nul 2>&1 
if %errorlevel%==0 (goto action)
type %output%num%.tmp | findstr /m "shut" >nul 2>&1 
if %errorlevel%==0 (goto action)

:action
type output%num%.tmp
del output%num%.tmp
exit /b 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM