如何在批处理文件中使用 set 和 if 语句
How to use set and if statements in batch files
所以我正在尝试用一些集合和 ifs 制作一个小批量程序,看起来很简单吧?显然,你不能像这样使用 "set" 和 "if"。
@echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
然后继续代码,稍后在程序中执行此操作。
If %variableTrue% EQU 1 goto next
请注意,我也尝试过使用感叹号。
带着感叹号,就好像它完全无视了这个声明,即使它是真的它也会照常继续。随着百分号,它在崩溃前说得非常快
此时预计不会出现“1”。
或者类似的东西,就像我说的那样,它几乎没有停留半秒钟。
我一直认为你可以这样做,只要没有冲突的变量。
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
这不会给出错误,它只是忽略该语句并照常继续。
更新:
修复错误后,这仍然不起作用。
当我使用感叹号时,它会忽略该行,而当我使用百分号时,它会说:
“
"1 was not expected at this time"
使用
set variableTrue=1
而不是
set variableTrue EQU 1
这个批处理脚本:
@echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
产出
A
C
一组问题是您的 IF 语句。例如,If "!coolestNum!"== 1 goto cool
。报价包含在比较中。
您需要对称 - 要么在两边都包含引号
If "!coolestNum!" == "1" goto cool
或两者都不是:
If !coolestNum! == 1 goto cool
If "!fn!" EQU 13 goto setvar
以及它之前的行存在同样的问题。
您遇到的另一个问题是 set "coolestNum==1"
有一个额外的、不需要的 =
。第二个 =
成为值的一部分。您只需要两个带 IF 比较的等号。
这里是更正后的代码:
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
但是你的逻辑不必要地复杂。以下产生完全相同的结果。
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b
所以我正在尝试用一些集合和 ifs 制作一个小批量程序,看起来很简单吧?显然,你不能像这样使用 "set" 和 "if"。
@echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
然后继续代码,稍后在程序中执行此操作。
If %variableTrue% EQU 1 goto next
请注意,我也尝试过使用感叹号。 带着感叹号,就好像它完全无视了这个声明,即使它是真的它也会照常继续。随着百分号,它在崩溃前说得非常快 此时预计不会出现“1”。 或者类似的东西,就像我说的那样,它几乎没有停留半秒钟。 我一直认为你可以这样做,只要没有冲突的变量。
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
这不会给出错误,它只是忽略该语句并照常继续。
更新: 修复错误后,这仍然不起作用。 当我使用感叹号时,它会忽略该行,而当我使用百分号时,它会说: “
"1 was not expected at this time"
使用
set variableTrue=1
而不是
set variableTrue EQU 1
这个批处理脚本:
@echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
产出
A
C
一组问题是您的 IF 语句。例如,If "!coolestNum!"== 1 goto cool
。报价包含在比较中。
您需要对称 - 要么在两边都包含引号
If "!coolestNum!" == "1" goto cool
或两者都不是:
If !coolestNum! == 1 goto cool
If "!fn!" EQU 13 goto setvar
以及它之前的行存在同样的问题。
您遇到的另一个问题是 set "coolestNum==1"
有一个额外的、不需要的 =
。第二个 =
成为值的一部分。您只需要两个带 IF 比较的等号。
这里是更正后的代码:
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
但是你的逻辑不必要地复杂。以下产生完全相同的结果。
:start
@echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b