batch-file-es Windows
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
- Page 35 and 36: El intérprete de comandos trata to
- Page 37 and 38: Lote Golpetazo Descripcióncommand
- Page 39 and 40: Lote Golpetazo Descripciónen valor
- Page 41 and 42: Capítulo 11: Directorio de pilaSin
- Page 43 and 44: Capítulo 12: EcoIntroducciónecho
- Page 45 and 46: > echo off> echoECHO is off.> echo
- Page 47 and 48: (This will output:if %example%=="He
- Page 49 and 50: SoluciónDe acuerdo con un set /? d
- Page 51 and 52: Capítulo 14: Escapar de personajes
- Page 53 and 54: Findstr viene con un montón de per
- Page 55 and 56: Capítulo 15: Evitar las limitacion
- Page 57 and 58: Capítulo 16: FuncionesObservacione
- Page 59 and 60: call :stuff 123 var1rem More code..
- Page 61 and 62: Capítulo 17: Híbridos por lotes y
- Page 63 and 64: Capítulo 18: Híbridos por lotes y
- Page 65 and 66: Capítulo 19: Macros de archivos po
- Page 67 and 68: Capítulo 20: Mandatos por lotes ob
- Page 69 and 70: Capítulo 21: Manejo de archivos en
- Page 71 and 72: Aquí hay un ejemplo simple:copy fo
- Page 73 and 74: Capítulo 22: Mejores prácticasInt
- Page 75 and 76: Ejemplo BConsidere las siguientes a
- Page 77 and 78: Un ejemplo más avanzado muestra c
- Page 79 and 80: Capítulo 24: Privilegios elevados
- Page 81 and 82: :: check if we have administrative
- Page 83 and 84: Capítulo 25: Redirección de entra
- Page 85: clsecho Please input the file path,
- Page 89 and 90: Capítulo 27: Utilizando GotoIntrod
- Page 91 and 92: Capítulo 28: Variables en archivos
- Page 93 and 94: ya que (como se ve, al ver la ejecu
- Page 95 and 96: echo Get the variable directly: %va
- Page 97 and 98: set /A Quotient=Divided / Divisorse
- Page 99 and 100: CreditosS.NoCapítulos Contributors
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