下,我是新来的一批脚本和感谢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