2017-05-29 131 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中的比较运算符是==而不是=