简体   繁体   English

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

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

hc.bat : hc.bat

Expand num like !num! num一样扩展!num! (delayed expansion). (延迟扩展)。
I modified the set /A statement to avoid (immediate) expansion right to = . 我修改了set /A语句,以避免(立即)扩展=权限。
The redirection > means to overwrite data. 重定向>表示覆盖数据。 Hence I changed it to >> in order to append data. 因此,为了附加数据,我将其更改为>>
Insert cmd /C into start /B argument. cmd /C插入start /B参数。
At findstr , the /b switch and ^ being the first char. findstr/b开关和^是第一个字符。 in the search string are redudant. 在搜索字符串中是多余的。 I gave the /r switch to force regular expression mode. 我给/r开关强制使用正则表达式模式。
The 3rd for /F token was not used, so I simply removed it. 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 : 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

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

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