繁体   English   中英

批处理脚本在执行后仍保存文件

[英]batch script still holding the file after execution

蝙蝠

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

脚本有两个问题:1)进程一直保存到日志文件check0.log 2)在hc.bat中,我试图使用findstr忽略以#开头的行,但似乎不是工作中

hc.bat

num一样扩展!num! (延迟扩展)。
我修改了set /A语句,以避免(立即)扩展=权限。
重定向>表示覆盖数据。 因此,为了附加数据,我将其更改为>>
cmd /C插入start /B参数。
findstr/b开关和^是第一个字符。 在搜索字符串中是多余的。 我给/r开关强制使用正则表达式模式。
for /F令牌的第3个未使用,因此我只是将其删除。

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

实际上,您不需要在这里延迟扩展(尽管它没有害处)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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