Windows批处理脚本,用于在

时间:2017-05-29 17:50:06

标签: windows batch-file logging cmd

我是批处理脚本的新手,感谢stackoverflow,我能够立刻编写一个脚本

我正在处理一个批处理脚本,它会并行触发其他批处理scriptlet(结合使用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
)

1 个答案:

答案 0 :(得分:0)

id default X1 X2 X3;
1 0 0 1 0
1 0 0 1 0
1 0 1 1 1
1 0 0 1 0
1 1 0 1 0
2 0 0 1 0
2 0 1 1 1
2 0 0 1 0
2 0 0 1 0
2 1 0 1 0
3 0 0 1 0
3 0 0 1 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的要求。

usebackqeol不是必需的,因为默认delims包含空格

在测试之前将delims设置为 nothing ,因为其值将保持不变。第一个文件将var设置为非空,因此在测试第二个文件之前需要清除它。语法var(其中value可以为空)用于确保任何杂散尾随空格不包含在分配的值中。

SET "var=value"重定向的目标应为findstr,而不是nul - 您会发现已创建名为null的文件。

null中的比较运算符为if而非==