1

So I generate manuals under windows using a batch file. The processing tool for the manuals has a flawed error reporting. I can't just use its error code. I additionally have to check the log for error.

To do this error check I want to use the following snippet in a batch file:

findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
if %ERRORLEVEL% NEQ 1 (
    rem do error handling stuff
)

When typing the findstr/find console command into a cmd.exe-window it does as expected and sets the expected ERRORLEVEL. It sets ERRORLEVEL to 0 if something is found. It sets ERRORLEVEL to 1 if nothing was found.

My problem: findstr/find behaves differently inside a batch file. It will set ERRORLEVEL to 0 even when nothing is found. It doesn't matter if the batch file is started by jenkins or if I start the batch file inside a cmd.exe-window.

I already tried:

  • find and findstr, both commands cause the same problem
  • checking if I run findstr on the correct file: Yes, it is the same file. I use absolute paths and made the batch file echo the file to be searched. When running findstr/find outside a batch file on the to be searched it sets ERRORLEVEL as expected.
  • checking if I run findstr with the same search string: Yes, it is the correct search string. I even tried to let findstr read the search-string from a file using the /G option.
  • a mixup with the Linux variant of the find command: It is not the Linux variant of the find command as findstr has the same behaviour.

Here is the complete batch file I use if soemone spots the cause for my problem somwhere else in the file. Thanks for your time.

set BEGIN_TIME=%TIME%

if bServerAdmin NEQ %USERNAME% (
    echo This file is designed to be run on the build server.
    echo Are you sure you want to run it?
    echo Press CTRL+C to abort
    echo Press Return to continue
    pause
)

rem ========================================
rem Set some variable that influence the run
rem ========================================
set HELP_AND_MANUAL="C:\Program Files\HelpAndManual\helpman.exe"
rem 7zip executable to use, http://www.7-zip.org/
set SEVEN_ZIP="C:\Program Files\7-Zip\7z.exe"

rem ===================================================
rem Make sure working directory exists and switch to it
rem ===================================================
if not exist output mkdir output
call :pushdWithErrCheck output

rem ===============
rem Clear old stuff
rem ===============
del /Q /S /F *

rem ======
rem German
rem ======
call :helpAndManualWithErrCheck XXXXXXX\XX\XXXXXXX.hmxp XXXXXXX.chm   xxxxx.hmskin
call :helpAndManualWithErrCheck AAAA\AA\AAAA.hmxp       AAAA\AAAA.chm xxxxx.hmskin
call :helpAndManualWithErrCheck BBBB\BB\BBBB.hmxp       BBBB\BBBB.chm xxxxx.hmskin
call :helpAndManualWithErrCheck CCCC\CC\CCCC.hmxp       CCCC\CCCC.chm xxxxx.hmskin

rem ======================
rem Pack all build results
rem ======================
%SEVEN_ZIP% a -bd ..\Manuale.7z
IF %ERRORLEVEL% NEQ 0 exit 1

popd

exit 0

:helpAndManualWithErrCheck 
IF EXIST %WORKSPACE%\%1 (
    echo building %1 now
    if exist %WORKSPACE%\output\%2 (
        echo Error: output file exists before build was started
        Exit 1
    )
    %HELP_AND_MANUAL% %WORKSPACE%\%1 /CHM=%WORKSPACE%\output\%2 /L="%WORKSPACE%\%2.log" /O=%WORKSPACE%\_Design\HTML-Skin\%3
    IF %ERRORLEVEL% NEQ 0 (
        IF %ERRORLEVEL% NEQ 2 (
            rem ERRORLEVEL 2 is not an error either http://www.helpandmanual.com/help/index.html?hm_advanced_commandline_exitcodes.htm
            echo Error: exiting due to bad exit code now
            Exit 1
        )
    )
    if not exist %WORKSPACE%\output\%2 (
        echo Error: exiting due to missing file now
        Exit 1
    )
    rem debugging stuff echo findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    rem debugging stuff findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    rem debugging stuff echo %ERRORLEVEL%
    findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    if %ERRORLEVEL% NEQ 1 (
        echo Error: exiting due to missing file according to HelpAndManual Log
        Exit 1
    )
) ELSE (
    echo Skipping %1 as the source file does not exist
)
goto :EOF

:pushdWithErrCheck 
pushd %1
if %ERRORLEVEL% NEQ 0 Exit 1
goto :EOF
user3331950
  • 119
  • 1
  • 6

1 Answers1

2

Your main problem is called normal vs delayed variable expansion

For the errorlvel test better use

findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
if errorlevel 1 (
    rem do error handling stuff
) 

Note that the expression if errorlevel n will be evaluated to true for any errorlevel value greater than or equal to n

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Yes, this seems to be the solution. I hat to use "not errorlevel 1" as a test condition but otherwise it works fine. Thank you very much. _omg i hate batch soo much it suck so terribly ... i mean sh and bash is flawed already but how can batch that much worse?_ – user3331950 Jul 13 '15 at 16:02
  • @user3331950 - if you hate batch that much, a good chunk of the people who browse the [batch-file] tag will tell you to use PowerShell instead. Something about it being more powerful and with aliases for linux users. Personally, it's not my thing, but a lot of people swear by it for doing command line stuff. – SomethingDark Jul 13 '15 at 17:19
  • But batch files are funny! The same not well defined operative rules that cause all these problems allows that batch files may be used in totally unplanned ways that are surprisingly interesting! This point just not exist in any "real programming language" that have better defined rules (they are boring). **`;-)`** – Aacini Jul 13 '15 at 18:07