error ejemplos batch batch-file cmd errorlevel

batch file - ejemplos - ¿Cuáles son los valores ERRORLEVEL establecidos por los comandos internos cmd.exe?



if errorlevel %== 0 (1)

ERRORLEVEL es un valor devuelto por la mayoría de los comandos cmd.exe cuando finalizan ese cambio dependiendo de una serie de condiciones, por lo que conocer el valor que devuelven los comandos es información valiosa que puede ayudar a escribir mejores archivos Batch. Todos los programas .exe externos cambian el ERRORLEVEL cuando finalizan (que es un mecanismo inherente de las funciones API ExitProcess y TerminateProcess Win-32) y generalmente dichos valores están documentados, pero los valores devueltos por los comandos internos cmd.exe no están completamente documentados en otro lugar .

En esta pregunta aparece una tabla con valores ERRORLEVEL parciales, pero solo para los comandos internos que establecen ERRORLEVEL = 0 "en caso de éxito". Sugerí que el OP de dicha pregunta lo modificara para incluir también los valores devueltos por "comandos no exitosos", pero él se negó y me invitó a publicar mi propia pregunta / respuesta, ¡así que aquí está! Debe tener en cuenta que un ERRORLEVEL diferente de cero no significa necesariamente que el comando haya fallado. Hay algunos comandos que terminan sin error y devuelven un valor mayor que cero para indicar un "estado de salida" diferente, incluidos los comandos internos (como SET /P ).

Para hacer un mejor uso de los comandos cmd.exe integrados en los archivos .bat de Batch, necesitamos conocer los valores de ERRORLEVEL que devuelven y los mecanismos involucrados en esta administración. Entonces, la pregunta es, ¿qué comandos internos de cmd.exe configuran ERRORLEVEL en algún valor (incluido cero)?


En esta respuesta, se describen los valores ERRORLEVEL devueltos por todos los comandos internos cmd.exe; se agrupan por la forma en que se cambia el valor y se presentan como tablas de referencia rápida. Revisé otras tablas similares para ensamblar esta, pero llené los valores faltantes a través de pruebas realizadas en una computadora con Windows 8.1. Hice mi mejor esfuerzo para crear estas tablas completas y precisas, pero no había probado todos y cada uno de los valores informados aquí, por lo que pueden ser inconsistencias sutiles.

Tabla 1 - Comandos que no cambian el valor ERRORLEVEL anterior

BREAK ECHO ENDLOCAL FOR Not change the ERRORLEVEL by itself. See "Exit Code" below. IF Not change the ERRORLEVEL by itself. PAUSE RD Not change the ERRORLEVEL on errors, but the "Exit Code". See below. REM RMDIR Same as RD. SET Plain SET command (no arguments). See "Table 3" below. TITLE

Tabla 2 - Comandos que establecen ERRORLEVEL en 0 o 1 dependiendo del resultado

Command │ Set ERRORLEVEL = 0 when │ Set ERRORLEVEL = 1 when ────────┼───────────────────────────────┼───────────────────────────────────────────────────────────── CD │Current directory was changed. │Directory not exists or is not accessible. CHDIR │Same as CD. │ COLOR │Color was changed. │Background and foreground colors are the same. COPY │File(s) was processed. │File not found or bad parameters given. DATE │Date was changed or not given. │User has no admin privileges. DEL │Almost always, excepting when: │Bad or no parameters given. DIR │Same as COPY. │ ERASE │Same as DEL. │ MD │Directory was created. │Directory could not be created. MKDIR │Same as MD. │ MKLINK │Link was created. │Link could not be created or bad parameters given. MOVE │File(s) was moved/renamed. │File not found, could not be moved/renamed or bad parameters. PUSHD │Same as CD. │+ Bad switch given. REN │Same as MOVE. │ RENAME │Same as MOVE. │ SETLOCAL│New environment was created. │Bad parameters given. TIME │Time was changed or not given. │User has no admin privileges. TYPE │Same as COPY. │ VERIFY │Right or no parameters given. │Bad parameters given. VOL │Volume label was displayed. │Drive not found or bad parameters given.

Tabla 3 - Comandos que establecen ERRORLEVEL en caso de error; de lo contrario, no lo cambie

Command │E│ Set ERRORLEVEL to = when ─────────────┼─┼──────────────────────────────────────────────────────────────────────── ASSOC │*│1 = Extension associations could not be changed. CLS │ │1 = Bad switch given. DPATH │*│1 = Data path could not be established. FTYPE │*│1 = File type associations could not be changed. GOTO label │ │1 = Label not exist *in a subroutine* (equivalent to: EXIT /B 1). KEYS │ │1 = Bad switch given. PATH │*│1 = Path could not be changed. POPD │ │1 = Bad switch given. PROMPT |*│1 = Prompt could not be changed. SET var │*│1 = No variable with such name exists. SET var=value│*│1 = Variable name start with "/" not enclosed in quotes. SET /P │*│1 = Read an empty line or at end of file. SET /A │*│1073750988 = Unbalanced parentheses, 1073750989 = Missing operand, │ │1073750990 = Syntax error, 1073750991 = Invalid number, │ │1073750992 = Number larger than 32-bits, 1073750993 = Division by zero. SHIFT │ │1 = Bad switch given.

La columna "E" en la Tabla 3 indica los comandos que cambian su comportamiento de acuerdo con el estado de "Extensiones" como se describe en la documentación correspondiente. Cuando las Extensiones están habilitadas (el valor predeterminado) y estos comandos se colocan en un archivo con extensión .CMD lugar de .BAT one, estos comandos establecen SETERRORLEVEL = 0 cuando finalizan sin error, es decir, cuando las condiciones descritas en la Tabla 3 son no presente

Tabla 4 - Casos especiales

CALL Table1 │If the called command is anyone of Table 1 (excepting FOR and IF): set ERRORLEVEL = 0. CALL subroutine │If the subroutine is called, not change prior ERRORLEVEL value; │otherwise (subroutine not exists): set ERRORLEVEL = 1. EXIT /B, EXIT │Not change prior ERRORLEVEL value. EXIT /B number │Set ERRORLEVEL to given number. EXIT number │Ends cmd.exe and set its returning ERRORLEVEL value to given number. START command │If command is started, not change ERRORLEVEL; otherwise, set ERRORLEVEL = 9059. START /WAIT bat |When the started Batch file end, set ERRORLEVEL = value from ''EXIT number'' commmand. notExist │If a non-existent command is entered for execution, set ERRORLEVEL = 9009. VER │Set ERRORLEVEL = 0 almost always. If /? parameter is given, not change ERRORLEVEL.

Gestión del código de salida

Hay dos formas de probar el valor ERRORLEVEL: mediante el command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0 IF ERRORLEVEL / IF %ERRORLEVEL% , o usando el command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0 command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0 constructo. Sin embargo, ciertos comandos particulares y errores de redireccionamiento devuelven un valor que solo funciona en el segundo caso y no se refleja en el ERRORLEVEL; podemos llamar a "Código de salida" este valor. Cuando este código de salida no es cero, se puede pasar al ERRORLEVEL ejecutando cualquier comando de la Tabla 1 en la parte elseCmd . Puede leer más detalles sobre este asunto en esta publicación .

Tabla 5 - Comandos o características que establecen el Código de salida

Feature │ Set Exit Code to = when ─────────────┼───────────────────────────────────────────────────────────────────────── command │1 = Command not exist (when ERRORLEVEL = 9009). redirection │1 = File not exists in "<", path not exists or access denied in ">" ">>". drive: |1 = Drive unit not exists. POPD |1 = No matching PUSHD was previously executed. RD │1 = Bad switch given, 2 = Directory not found, 5 = Access denied, │32 = Directory in use, 145 = Directory not empty. FOR /F │1 = No data was processed.

Por ejemplo, para probar si ocurrió un error de redireccionamiento, use esto:

command > C:/Path/that/does/not/exist/file.txt || rem if errorlevel 1 echo Previous redirection failed

En este ejemplo, el comando rem se usa para copiar el código de salida al ERRORLEVEL, pero se puede usar cualquier otro comando interno que conserve el ERRORLEVEL (excepto FOR e IF ).

Para probar si existe una unidad de accionamiento:

U: || rem if errorlevel 1 echo Previous set current drive to U: unit failed

Más ejemplos:

rd c:/Some/directory 2> NUL || rem if %errorlevel% equ 0 ( echo Directory deleted ) else if %errorlevel% equ 2 ( echo Directory not found ) else if %errorlevel% equ 5 ( echo Can not access the directory, check rights ) else if %errorlevel% equ 32 ( echo Can not delete current directory ) else if %errorlevel% equ 145 ( echo Directory is not empty, use /S switch ) (for /F "options" %%a in (input.txt) do echo %%a) || rem if errorlevel 1 echo Previous FOR didn''t processed any value