0

I’m new to batch scripting and thanks to stackoverflow, I’m able to put together a script with no time

I’m working on a batch script which triggers other batch scriptlets in parallel (with combination of start + cmd) generating individual logs, wait for them to complete and collate them into a master log.

The condition i’m using is that every log file ends with a keyword “Elapsed” the master scripts checks if each log ends with the keyword and moves the log to the masterlog, else moves to a timeout state and repeats the process again. It work fine for the first attempts but fails read the last line of rest of the files (ch2.log ch3.log and ch4.log) and copies them without checking. Could you please let me know what I am missing?

Here is the part of the script which has the logic

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
)
Prabhu R
  • 1
  • 1
  • 3
    Really? This code works for you? That very first `for` loop has at least three different things wrong with it. It's missing a second `'`, the first `'` is a `‘` instead of a `'`, and there's no `)`. – SomethingDark May 29 '17 at 18:04
  • I copied this over ms word that might be the reason for ‘ instead of an ' and yes the script works fine but only for the first log file. I have edited the question. hope it looks good now – Prabhu R May 29 '17 at 18:16
  • Remove the line `set %fname% =%%i` and replace every instance of `%fname%` with `%%i`. This looks like it's related to [delayed expansion](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set). (Or stick the line `setlocal enabledelayedexpansion` at the top of the script and use `!fname!` instead of `%fname%`, but using `%%i` is cleaner IMO.) – SomethingDark May 29 '17 at 18:27
  • You can't use labels within a `code block` (parenthesised series of lines). – Magoo May 29 '17 at 18:45

1 Answers1

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

Note: Since the filename is being supplied to the :wait routine as a quoted string, %1 will be quoted hence requirement for usebackq.

eol and delims are not required as the default delims includes Space

set var to nothing before the test as its value will persist. the first file will set var to not-empty, so it needs t be cleared before the second file is tested. The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.

the destination for findstr redirection should be nul, not null - you will find that a file named null was created.

The comparison operator in an if is == not =.

Magoo
  • 77,302
  • 8
  • 62
  • 84