下,我是新來的一批腳本和感謝StackOverflow的,我能夠把一個腳本沒有時間Windows批處理腳本來整理日誌文件到主日誌條件
我工作的一個批次腳本,並行觸發其他批處理腳本(與start + cmd結合)生成單個日誌,等待它們完成並將它們整理到主日誌中。
我使用的條件是每個日誌文件都以關鍵字「Elapsed」結尾,主腳本檢查每個日誌是否以關鍵字結尾,並將日誌移至masterlog,否則移至超時狀態並重復再次處理。它在第一次嘗試時工作正常,但未能讀取其餘文件的最後一行(ch2.log ch3.log和ch4.log),並且未經檢查就複製它們。你能讓我知道我失蹤了嗎?
這裏是一個具有邏輯
for /f %%i in ('dir /b ch*.log') do (
REM display the list of logs (in this case it's ch1.log ch2.log ch3.log ch4.log)
set %fname% =%%i
:ctout
timeout 20>nul
REM wait until the timer runs out
for /f delims ^=^ eol^=%%l in (%fname%) do set lastline=%%l
REM check for the last line of the file and set the last line of the log as 'lastline'
echo %lastline% | findstr /i "\<Elapsed\>" >null && set var=elapsed
REM check if the lastline has the word "Elapsed", which marks the end of file and assign a dummy variable
if not "%var%"="elapsed" goto :ctout
REM check if the variable is "elapsed" else goto ctout
type %fname% >> masterlog.txt
REM if the condition satisfies the contents of ch1.log is moved to masterlog.txt
del /s %fname% >nul 2>nul
REM deletes the logs from the list and moves to the next log file
)
真的嗎?這段代碼適合你嗎?第一個for循環至少有三個不同的錯誤。它缺少第二個''',第一個'''是'''而不是''',並且沒有''''。 – SomethingDark
我把它複製到ms字上,可能是'而不是'的原因,是的腳本工作正常,但只適用於第一個日誌文件。我編輯了這個問題。希望現在看起來不錯 –
刪除'set%fname%= %% i'這行,並用'%% i'替換'%fname%'的每個實例。這看起來像是[延遲擴展](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set)。 (或者在腳本的頂部加上'setlocal enabledelayedexpansion'行並使用'!fname!'而不是'%fname%',但使用'%% i'則清潔IMO。) – SomethingDark