【问题标题】:How to handle errors using batch commands?如何使用批处理命令处理错误?
【发布时间】:2015-03-19 14:38:14
【问题描述】:

我正在尝试使用以下命令删除超过 14 天的文件:forfiles /P "Path\To\Files" /S /M *.txt /D -14 /C "cmd /c del @path"

这是我在尝试删除文件时遇到的错误:ERROR: No files found with the specified search criteria.

有没有办法处理或抑制这个错误?原因是因为我在 jenkins 上使用了这个命令,并且由于这个命令而构建失败。谢谢。

我还查看了 ERRORLEVEL 并使用了以下代码,但它仍然给了我错误:

forfiles /P "Path\To\Files" /S /M *.txt /D -14 /C "cmd /c del @path" | find "ERROR" >nul2>nul
if not ERRORLEVEL 1 (
echo. Uh oh, something bad happened
exit /b 1
)

【问题讨论】:

    标签: batch-file command-line jenkins cmd windows-console


    【解决方案1】:

    您可以创建一个 goto 标签来检查错误。只需将call:checkerrors 放在任何需要检查错误的行之后。

    forfiles /P "Path\To\Files" /S /M *.txt /D -14 /C "cmd /c del @path"
    call:checkerrors
    
    goto :eof
    
    :checkerrors
    if %errorlevel% neq 0 (
        echo. Uh oh, something bad happened
        exit /b 0
    )
    
    goto :eof
    

    【讨论】:

    • 因为除了在屏幕上打印一些文本之外,您不会对错误执行任何操作,您只需将 exit /b 0 放在您的 forfiles 行之后。 Jenkins 使用批处理脚本中的错误代码。默认情况下,批处理脚本中的错误级别是批处理脚本中最后一个命令的错误级别。 exit 可用于将错误级别设置为特定值。
    【解决方案2】:

    如果 ERROREVEL 不为 0,则认为构建失败。

    forfiles /P "Path\To\Files" /S /M *.txt /D -14 /C "cmd /c del @path" | find "ERROR" >nul2>nul
    if not ERRORLEVEL 0 (
    echo. Uh oh, something bad happened
    exit /b 0
    )
    

    对于像 Gnatcheck 或其他系统地返回与 0 不同的 ERRORLEVEL 的工具来说,这也是问题所在,而 Jenkins 正在等待 0 继续没有错误。

    【讨论】:

      【解决方案3】:

      您可以解决此错误,保留所有其他 ERROR 消息:

      forfiles /P "Path\To\Files" /S /M *.txt /D -14 /C "cmd /c del @path" 2>&1|find /v "ERROR: No files found with the specified search criteria." | find "ERROR" >nul2>nul
      

      EDIT 只有 STDOUT 被传送到 find,因此错误消息保留在屏幕上。编辑将 STDERR 也写入 STDOUT,以便find可以处理它。

      【讨论】:

      • 感谢您的回复,但这个答案仍然给我这个错误:ERROR: No files found with the specified search criteria.
      【解决方案4】:

      当我自己遇到同样的问题时,我找到了一些方法来满足您的要求 - 在其他地方发布了几次。我发现其他解决方案可能会删除实际的错误文本,但忽略了表示我的应用程序失败的 %ERRORLEVEL% 。并且我合法地想要 %ERRORLEVEL%,只要它不是“未找到文件”错误。

      一些例子:

      具体调试和排除错误:

      forfiles /p "[file path...]\IDOC_ARCHIVE" /s /m *.txt /d -1 /c "cmd /c del @path" 2>&1 |  findstr /V /O /C:"ERROR: No files found with the specified search criteria."2>&1 | findstr ERROR&&ECHO found error||echo found success
      

      使用oneliner返回ERRORLEVEL成功或失败:

      forfiles /p "[file path...]\IDOC_ARCHIVE" /s /m *.txt /d -1 /c "cmd /c del @path" 2>&1 |  findstr /V /O /C:"ERROR: No files found with the specified search criteria."2>&1 | findstr ERROR&&EXIT /B 1||EXIT /B 0
      

      对于 SQL Server 代理 CmdExec 作业步骤,我执行了以下操作。我不知道这是否是一个错误,但步骤中的 CmdExec 只识别第一行代码。另外,CmdExec 不喜欢 2>&1 来重定向 stderr,所以我用 cmd /c 封装了所有内容:

      cmd /e:on /c "forfiles /p "C:\SQLADMIN\MAINTREPORTS\SQL2" /s /m *.txt /d -1 /c "cmd /c del @path" 2>&1 |  findstr /V /O /C:"ERROR: No files found with the specified search criteria."2>&1 | findstr ERROR&&EXIT 1||EXIT 0"&exit %errorlevel%
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 2019-09-03
        • 1970-01-01
        • 2018-01-27
        • 2020-08-31
        • 1970-01-01
        相关资源
        最近更新 更多