Windows findstr/find commands behave different regarding ERRORLEVEL when run inside and outside a batch file -


so generate manuals under windows using batch file. processing tool manuals has flawed error reporting. can't use error code. additionally have check log error.

to error check want use following snippet in batch file:

findstr /l /i /c:" not found in " "%workspace%\%2.log" if %errorlevel% neq 1 (     rem error handling stuff ) 

when typing findstr/find console command cmd.exe-window expected , sets expected errorlevel. sets errorlevel 0 if found. sets errorlevel 1 if nothing found.

my problem: findstr/find behaves differently inside batch file. set errorlevel 0 when nothing found. doesn't matter if batch file started jenkins or if start batch file inside cmd.exe-window.

i tried:

  • find , findstr, both commands cause same problem
  • checking if run findstr on correct file: yes, same file. use absolute paths , made batch file echo file searched. when running findstr/find outside batch file on searched sets errorlevel expected.
  • checking if run findstr same search string: yes, correct search string. tried let findstr read search-string file using /g option.
  • a mixup linux variant of find command: not linux variant of find command findstr has same behaviour.

here complete batch file use if soemone spots cause problem somwhere else in file. time.

set begin_time=%time%  if bserveradmin neq %username% (     echo file designed run on build server.     echo sure want run it?     echo press ctrl+c abort     echo press return continue     pause )  rem ======================================== rem set variable influence run rem ======================================== set help_and_manual="c:\program files\helpandmanual\helpman.exe" rem 7zip executable use, http://www.7-zip.org/ set seven_zip="c:\program files\7-zip\7z.exe"  rem =================================================== rem make sure working directory exists , switch 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 build results rem ====================== %seven_zip% -bd ..\manuale.7z if %errorlevel% neq 0 exit 1  popd  exit 0  :helpandmanualwitherrcheck  if exist %workspace%\%1 (     echo building %1     if exist %workspace%\output\%2 (         echo error: output file exists before build 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 not error either http://www.helpandmanual.com/help/index.html?hm_advanced_commandline_exitcodes.htm             echo error: exiting due bad exit code             exit 1         )     )     if not exist %workspace%\output\%2 (         echo error: exiting due missing file         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 missing file according helpandmanual log         exit 1     ) ) else (     echo skipping %1 source file not exist ) goto :eof  :pushdwitherrcheck  pushd %1 if %errorlevel% neq 0 exit 1 goto :eof 

your main problem called normal vs delayed variable expansion

for errorlvel test better use

findstr /l /i /c:" not found in " "%workspace%\%2.log" if errorlevel 1 (     rem error handling stuff )  

note expression if errorlevel n evaluated true errorlevel value greater or equal n


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -