简体   繁体   English

如何使用批处理命令处理错误?

[英]How to handle errors using batch commands?

I am trying to delete files that are older than 14 days using this command: forfiles /P "Path\\To\\Files" /S /M *.txt /D -14 /C "cmd /c del @path" 我正在尝试使用以下命令删除早于14天的文件: forfiles /P "Path\\To\\Files" /S /M *.txt /D -14 /C "cmd /c del @path"

This is the error I get when trying to delete files : ERROR: No files found with the specified search criteria. 这是我在尝试删除文件时遇到的错误: ERROR: No files found with the specified search criteria.

Is there a way to handle this error or suppress it? 有没有办法处理或抑制此错误? The reason being because I am using this command on jenkins and the build fails because of this command. 原因是因为我在jenkins上使用了此命令,并且由于该命令构建失败。 Thanks. 谢谢。

I also looked at ERRORLEVEL and use the following code but it still gave me the error: 我还查看了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
)

You can create a goto label to check for errors. 您可以创建一个goto标签来检查错误。 Simply place call:checkerrors after any line that needs errors to be checked. 只需将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

you can sort this error out, keeping all other ERROR messages: 您可以解决此错误,并保留所有其他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 only STDOUT was piped to find , so the errormessage kept on screen. 仅通过STDOUT 编辑 find ,因此错误消息一直显示在屏幕上。 Edited to write STDERR also to STDOUT, so that find can process it. 编辑后还可以将STDERR写入STDOUT,以便find可以对其进行处理。

if ERROREVEL different from 0, the build is considered as a failure. 如果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
)

This is also the issue for tools like Gnatcheck or other that systematically return an ERRORLEVEL diferent from 0 where Jenkins is waiting 0 to continue without mistakes. 对于Gnatcheck之类的工具或其他系统地从0返回ERRORLEVEL的工具(这也是Jenkins等待0继续进行而没有出错)的问题,这也是一个问题。

Here's some ways I've found to do just what you're asking as I ran into the same issue myself--posted a few times in other places. 当我自己遇到同一问题时,我发现有一些方法可以按照您的要求去做-在其他地方发布了几次。 I'm finding that other solutions might get rid of the actual error text but are ignoring the %ERRORLEVEL% which signals a fail in my application. 我发现其他解决方案可能会摆脱实际的错误文本,但忽略了%ERRORLEVEL%,这表明我的应用程序失败。 AND I legitimately want %ERRORLEVEL% just as long as it isn't the "No files found" error. 并且我合理地希望%ERRORLEVEL%,只要它不是“找不到文件”错误即可。

Some Examples: 一些例子:

Debugging and eliminating the error specifically: 专门调试和消除错误:

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

Using a oneliner to return ERRORLEVEL success or failure: 使用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

For a SQL Server Agent CmdExec job step I landed on the following. 对于SQL Server代理CmdExec作业,我进入以下步骤。 I don't know if it's a bug, but the CmdExec within the step only recognizes the first line of code. 我不知道这是否是一个错误,但是该步骤中的CmdExec仅识别第一行代码。 Also, CmdExec didn't like 2>&1 to redirect stderr so I encapsulated everything with cmd /c.: 另外,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%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM