簡體   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