batch-file-es Windows

carlos.wilber.franco
from carlos.wilber.franco More from this publisher
08.02.2022 Views

Capítulo 26: Si declaracionesSintaxis• if [/ i] StringToCompare1 == StringToCompare2 (commandA) else (commandB)• si errorlevel 1 (commandA) else (commandB)• si% errorlevel% == 1 (commandA) else (commandB)• si existe Nombre de archivo (commandA) else (commandB)• si se define VariableName (commandA) else (commandB)ObservacionesHay algunas sintaxis para elegir en una sentencia if . Usaremos if string1==string2 comoejemplo.Sintaxis de 1 línea• if string1==string2 commandA•if string1==string2 (commandA)• if string1==string2 (commandA) else (commandB)• if string1==string2 (commandA) else commandB• if string1==string2 (commandA)else (commandB)• if string1==string2 (commandA)else commandBSintaxis multilíneaif string1==string2 (commandA)Oif string1==string2 (commandA) else (commandB)Todavía hay algunas sintaxis adicionales disponibles.https://riptutorial.com/es/home 77

ExamplesComparando números con la declaración IFSET TEST=0IF %TEST% == 0 (echo TEST FAILED) ELSE IF %TEST% == 1 (echo TEST PASSED) ELSE (echo TEST INVALID)Comparando cuerdasIF "%~1" == "-help" (ECHO "Hello")donde %1 refiere al primer argumento de la línea de comando y ~ elimina todas las comillas que seincluyeron cuando se llamó al script.Comparando ErrorlevelIf Errorlevel 1 (Echo Errorlevel is 1 or higher)REM The phrase "1 or higher" is used because If Errorlevel 1 statement means:REM If %Errorlevel% GEQ 1REM Not If %Errorlevel% EQU 1oIf "%Errorlevel%"=="1" (Echo Errorlevel is 1)El script anterior verificará la variable Errorlevel (incorporada). El operador not puede ser utilizado.Set "Test=%Errorlevel%"If "%Test%" == "1" (Echo Errorlevel is 1)Este también funciona.Tenga en cuenta que algunos comandos no afectan el nivel de error :https://riptutorial.com/es/home 78

Examples

Comparando números con la declaración IF

SET TEST=0

IF %TEST% == 0 (

echo TEST FAILED

) ELSE IF %TEST% == 1 (

echo TEST PASSED

) ELSE (

echo TEST INVALID

)

Comparando cuerdas

IF "%~1" == "-help" (

ECHO "Hello"

)

donde %1 refiere al primer argumento de la línea de comando y ~ elimina todas las comillas que se

incluyeron cuando se llamó al script.

Comparando Errorlevel

If Errorlevel 1 (

Echo Errorlevel is 1 or higher

)

REM The phrase "1 or higher" is used because If Errorlevel 1 statement means:

REM If %Errorlevel% GEQ 1

REM Not If %Errorlevel% EQU 1

o

If "%Errorlevel%"=="1" (

Echo Errorlevel is 1

)

El script anterior verificará la variable Errorlevel (incorporada). El operador not puede ser utilizado.

Set "Test=%Errorlevel%"

If "%Test%" == "1" (

Echo Errorlevel is 1

)

Este también funciona.

Tenga en cuenta que algunos comandos no afectan el nivel de error :

https://riptutorial.com/es/home 78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!