Windows 批处理文件包含所有错误级别

标签 windows batch-file xcopy errorlevel

我有一个批处理文件,它只执行一系列复制和 xcopy 命令,如果其中任何一个失败,我需要跳出复制到 goto 标签,但是每次执行后都必须检查错误级别,这将非常不方便单份。

我怀疑这可能是不可能的,但是有没有一种方法可以让我进行大量的复制/xcopy,并在最后检查错误级别是否超过零?

最佳答案

您可以定义一个变量来充当简单的“宏”。节省了大量的打字时间,而且看起来也不错。

@echo off
setlocal
set "copy=if errorlevel 1 (goto :error) else copy"
set "xcopy=if errorlevel 1 (goto :error) else xcopy"

%copy% "somepath\file1" "location"
%copy% "somepath\file2" "location"
%xcopy% /s "sourcePath\*" "location2"
rem etc.
exit /b

:error
rem Handle your error

编辑

这是一个更通用的宏版本,应该适用于任何命令。请注意,宏解决方案比使用 CALL 快得多。

@echo off
setlocal
set "ifNoErr=if errorlevel 1 (goto :error) else "

%ifNoErr% copy "somepath\file1" "location"
%ifNoErr% copy "somepath\file2" "location"
%ifNoErr% xcopy /s "sourcePath\*" "location2"
rem etc.
exit /b

:error
rem Handle your error

关于Windows 批处理文件包含所有错误级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9279068/

相关文章:

Linux 命令行更改 Windows 可执行程序集信息

windows - SQL 加载程序 : writing batch file (windows) for processing multiple data file

visual-studio - Visual Studio 生成后事件 - xcopy 到不同的相对目录

c# - 将特定文件或文件夹添加到 Windows 资源管理器 View

c++ - 内存删除的运行时检测

windows - Windows 批处理文件中的三重 IF 语句?如果如果如果如果?其他错误

windows - 批处理文件 - 将目录更改为子文件夹

c# - 如何将路径作为参数传递给 Process.Start

powershell - 如果不存在,如何将更新的文件复制到新文件夹并创建目录作为源?

python - 从 spacy 中的存储模型加载后 doc.vector 不工作