gpt4 book ai didi

batch-file - 批处理脚本在失败时不提高 ERRORLEVEL

转载 作者:行者123 更新时间:2023-12-03 16:50:07 24 4
gpt4 key购买 nike

我正在学习 Windows 批处理脚本。当复制、移动或删除操作失败时,我需要提出错误。我制作了一个示例脚本,由于某种原因,当操作失败时,我无法让 ERRORLEVEL 上升。我运行脚本,文件不存在或在另一个程序中打开,stderr 消息输出到控制台,但 ERRORLEVEL 从未上升,这是为什么?另外,如果我无法让 ERRORLEVEL 上升,有没有什么方法可以将 stderr 传递到我可以检查的变量中?

我的代码如下:

`@Echo Off
ECHO.
Set /P myVar=Please enter a value:
Echo var = %myVar%
ECHO Trying to delete the file dummy.csv >> log.txt
Set myVar2 = nothing
DEL C:\dummy420.csv
IF NOT ERRORLEVEL 0 ECHO There was an error
ECHO %ERRORLEVEL%
REM 2>myVar2
REM Echo 2>
REM Echo %myVar2%
Echo 2>&1
REM && (echo yourCommand was successful) || (echo yourCommand failed)
IF NOT ERRORLEVEL 0 ECHO There was an error
ECHO %ERRORLEVEL%
Move ""C:\dummy420.csv" D:\"
IF NOT ERRORLEVEL 0 ECHO There was an error
Set /P dummy=Press Enter to End`

这是我的输出:

Please enter a value:hello
var = hello
C:\dummy420.csv
The process cannot access the file because it is being used by another process.
0
ECHO is off.
0
The filename, directory name, or volume label syntax is incorrect.
Press Enter to End

最佳答案

DEL 命令不会更改错误级别 - more info检查 debenham 建议以检测删除失败:

3>&2 2>&1 1>&3 del C:\dummy420.csv|findstr . && echo DEL failed || echo DEL succeeded

更改错误级别:

3>&2 2>&1 1>&3 del C:\dummy420.csv|findstr .&& cmd /c exit /b 1

关于batch-file - 批处理脚本在失败时不提高 ERRORLEVEL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31077490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com