2017-05-29 132 views
0

下,我是新來的一批腳本和感謝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 
) 
+3

真的嗎?這段代碼適合你嗎?第一個for循環至少有三個不同的錯誤。它缺少第二個''',第一個'''是'''而不是''',並且沒有''''。 – SomethingDark

+0

我把它複製到ms字上,可能是'而不是'的原因,是的腳本工作正常,但只適用於第一個日誌文件。我編輯了這個問題。希望現在看起來不錯 –

+0

刪除'set%fname%= %% i'這行,並用'%% i'替換'%fname%'的每個實例。這看起來像是[延遲擴展](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set)。 (或者在腳本的頂部加上'setlocal enabledelayedexpansion'行並使用'!fname!'而不是'%fname%',但使用'%% i'則清潔IMO。) – SomethingDark

回答

0
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) 
call :wait "%%i" 
) 
rem ... any remaining instructions after concatenating logs here 
goto :eof 
rem Jumps over the internal subroutine ":wait" 

:wait 
timeout 20>nul 
REM wait until the timer runs out 
for /f "usebackq" %%l in (%1) do set lastline=%%l 
REM check for the last line of the file and set the last line of the log as 'lastline' 
set "var=" 
echo %lastline% | findstr /i "\<Elapsed\>" >nul && 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 wait 
REM check if the variable is "elapsed" else goto wait 
>> masterlog.txt type %1 
REM if the condition satisfies the contents of ch?.log is moved to masterlog.txt 
del /s %1 >nul 2>nul 
REM deletes the logs from the list and moves to the next log file 
goto :eof 

註腳本的一部分:由於文件名被供應至所述:wait例程作爲引用字符串,%1將被引用爲usebackq因此要求。

eoldelims不需要爲默認delims包括空間

測試作爲它的價值會持續之前設置var什麼。第一個文件會將var設置爲非空,因此在測試第二個文件之前不需要清除它。語法SET "var=value"(其中值可能爲空)用於確保分配的值中不包含任何雜散結尾空格。

findstr重定向的目標應該是nul,不null - 你會發現這是創建了一個名爲null文件。

if中的比較運算符是==而不是=