在批处理文件内部和外部运行时,Windows findstr / find命令与ERRORLEVEL的行为不同

时间:2015-07-13 15:27:11

标签: batch-file find findstr errorlevel

因此,我使用批处理文件在Windows下生成手册。手册的处理工具有一个有缺陷的错误报告。我不能只使用它的错误代码。我还必须检查日志是否有错误。

要执行此错误,请检查我是否要在批处理文件中使用以下代码段:

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

在cmd.exe窗口中键入findstr / find console命令时,它会按预期执行并设置预期的ERRORLEVEL。如果找到了什么,它会将ERRORLEVEL设置为0。如果没有找到任何内容,它会将ERRORLEVEL设置为1。

我的问题:在批处理文件中,findstr / find的行为有所不同。即使没有找到任何内容,它也会将ERRORLEVEL设置为0。如果批处理文件是由jenkins启动的,或者我是否在cmd.exe窗口中启动批处理文件,则无关紧要。

我已经尝试过:

  • find和findstr,这两个命令都会导致同样的问题
  • 检查我是否在正确的文件上运行findstr:是的,它是同一个文件。我使用绝对路径并使批处理文件回显要搜索的文件。当在要搜索的批处理文件外运行findstr / find时,它会按预期设置ERRORLEVEL。
  • 检查我是否使用相同的搜索字符串运行findstr:是的,它是正确的搜索字符串。我甚至试图让findstr使用/ G选项从文件中读取搜索字符串。
  • 与find命令的Linux变体混合:它不是find命令的Linux变体,因为findstr具有相同的行为。

这是我使用的完整批处理文件,如果soemone发现我的问题的原因在文件中的其他地方。谢谢你的时间。

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

1 个答案:

答案 0 :(得分:2)

您的主要问题称为normal vs delayed variable expansion

更好地使用errorlvel测试

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

请注意,对于大于或等于if errorlevel n的任何errorlevel值,表达式n的评估结果为true