批处理文件中的多个 IF-Else 条件

批处理文件中的多个 IF-Else 条件

最近我问了一个关于如何在批处理文件中使用 if-else 检查的问题,这个问题已经解决了。现在我尝试通过在批处理文件中使用多个 if-else 条件来扩展这个问题。执行它时,什么也没发生。

为了理解我想要完成的任务:我有一个具有 4 种不同状态的 .ini 文件。我希望我的批处理脚本检查每个状态并针对相应的情况执行任务。为了进行测试,我设置了一个命令来创建一个文件夹,但准备就绪后它应该执行其他任务。但我认为这对我的问题并不重要。这是我到目前为止的代码:

  @echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%G in ('findstr "lang=" conf.ini') do (
    if "%%G"=="deu" (
        for /f "tokens=2 delims==" %%G in ('findstr "vers=" conf.ini') do (
            if "%%G"=="orig" (
                for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do (
                    if "%%G"=="0" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOff
                                goto :done
                                )
                            if "%%G"=="1" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOn
                                )
                                goto :done
                                )
                    if "%%G"=="1" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOff
                                goto :done
                                )
                            if "%%G"=="1" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOn
                                )
                                goto :done
                            )
            if  "%%G"=="rem" (
                for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do (
                    if "%%G"=="0" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOff
                                goto :done
                                )
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOn
                                )
                                goto :done
                            )
                    if "%%G"=="1" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOff
                                goto :done
                                )
                            if "%%G"=="1" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOn
                                )
                                goto :done
                            )
  if "%%G"=="deu" (
        for /f "tokens=2 delims==" %%G in ('findstr "vers=" conf.ini') do (
            if "%%G"=="orig" (
                for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do (
                    if "%%G"=="0" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOff
                                )
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOn
                                )
                                goto :done
                            )
                    if "%%G"=="1" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOff
                                goto :done
                                )
                            if "%%G"=="1" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOn
                                )
                                goto :done
                            )
            if  "%%G"=="rem" (
                for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do (
                    if "%%G"=="0" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOff
                                goto :done
                                )
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOn
                                )
                                goto :done
                            )
                    if "%%G"=="1" (
                        for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do (
                            if "%%G"=="0" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOff
                                goto :done
                                )
                            if "%%G"=="1" (
                                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOn
                                )
                                goto :done
                            )
:done
endlocal
    

编辑:由于评论中建议我展示我的 conf.ini 文件:

lang=deu
vers=orig
enhancements=0
modifications=0

脚本应该检查所有四个条目并执行彼此不同的任务。出于测试原因,它应该只创建一个文件夹。首先,它应该检查语言 (lang),然后继续转到 ger 或 eng。对于这两种情况,它都应该检查版本 (vers)。之后(对于这两种情况再次检查),它应该检查增强功能,并且应该对最后一行重复相同的过程。因此,我有 16 种可能的结果

编辑 2:这些是我期望的结果。为了进行测试,它应该启动预定义的沐浴脚本:

Case 1
lang=ger
vers=orig
enhancements=0
modifications=0
=> Start case1.bat

Case 2
lang=ger
vers=orig
enhancements=0
modifications=1
=> Start case2.bat

Case 3
lang=ger
vers=orig
enhancements=1
modifications=0
=> Start case3.bat

Case 4
lang=ger
vers=orig
enhancements=1
modifications=1
=> Start case4.bat

Case 5
lang=ger
vers=rem
enhancements=0
modifications=0
=> Start case5.bat

Case 6
lang=ger
vers=rem
enhancements=0
modifications=1
=> CStart case6.bat

Case 7
lang=ger
vers=rem
enhancements=1
modifications=0
=> Start case7.bat

Case 8
lang=eng
vers=rem
enhancements=1
modifications=1
=> Start case8.bat

Case 9
lang=eng
vers=orig
enhancements=0
modifications=0
=> Start case9.bat

Case 10
lang=eng
vers=orig
enhancements=0
modifications=1
=> Start case10.bat

Case 11
lang=eng
vers=orig
enhancements=1
modifications=0
=> Start case11.bat

Case 12
lang=eng
vers=orig
enhancements=1
modifications=1
=> Start case12.bat

Case 13
lang=eng
vers=rem
enhancements=0
modifications=0
=> Start case13.bat

Case 14
lang=eng
vers=rem
enhancements=0
modifications=1
=> Start case14.bat

Case 15
lang=eng
vers=rem
enhancements=1
modifications=0
=> Start case15.bat

Case 16
lang=eng
vers=rem
enhancements=1
modifications=1
=> Start case16.bat

答案1

与使用嵌套循环相比,提取和处理彼此独立的值更加容易。

@echo on

setlocal enabledelayedexpansion

REM Extract values from the file to individual variables independent from each other.

for /f "tokens=2 delims==" %%G in ('findstr "lang=" conf.ini') do set LNG=%%G
for /f "tokens=2 delims==" %%G in ('findstr "vers=" conf.ini') do set VER=%%G
for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do set ENH=%%G
for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do set MOD=%%G

REM Derive 4 parts of the directory name from individual values independent from each other.

if "%LNG%"=="deu"  set XL=DeutscheSprache
if "%LNG%"=="eng"  set XL=EnglischeSprache

if "%VER%"=="orig" set XV=Original
if "%VER%"=="rem"  set XV=Remastered

if "%ENH%"=="0"    set XE=EnhancementsOff
if "%ENH%"=="1"    set XE=EnhancementsOn

if "%MOD%"=="0"    set XM=ModificationsOff
if "%MOD%"=="1"    set XM=ModificationsOn

REM Create directory name based on the parts defined above.

set DIR=%XL%.%XV%.%XE%.%XM%

REM (Using "echo mkdir" instead of "mkdir for testing only.)

echo mkdir %DIR%

REM Other possibilities to answer the comment about running multiple commands instead of a single "set" command.

REM Depending on your requirements you can also run multiple commands
REM or check combinations of values using a single "if" statement.
REM Example:

if "%LNG%.%VER%.%ENH%.%MOD%"=="deu.orig.0.0" (
    echo This is case DeutscheSprache.Original.EnhancementsOff.modificationsOff
    echo foo
    echo bar
)
if "%LNG%.%VER%.%ENH%.%MOD%"=="deu.orig.0.1" (
    echo This is case DeutscheSprache.Original.EnhancementsOff.modificationsOn
    echo bar
    echo baz
)

REM etc...

endlocal

如果未找到预期模式或配置包含意外值,则此脚本不会处理错误。

答案2

  • 您可以if()else()使用以下方式无条件地执行此操作Findstr与运营商&& ||
@echo off

setlocal enabledelayedexpansion
cd /d "%~dp0" && set "_01=Enhancements,Modifications"

for %%i in (%_01%)do %__AppDir__%Findstr.exe /ibl %%~i=0 <.\conf.ini >nul && (
     set "_%%~i=%%~iOff" && %comSpec% /v:on /q /c "set^ "_%%~i=!_%%~i!"") || (
     set "_%%~i=%%~iOn". && %comSpec% /v:on /q /c "set^ "_%%~i=!_%%~i!"")

2>nul >con: %__AppDir__%Findstr.exe /ibl "vers=rem" <.\conf.ini >nul && (
     set "_dir=Sprache.Remastered.!_Enhancements!.!_Modifications!") || (
     set "_dir=Sprache.Original.!_Enhancements!.!_Modifications!"..)
      
2>nul >con: %__AppDir__%Findstr.exe /ibl "lang=deu" <.\conf.ini >nul && (
     2>nul dir/ad/b .\Deutsche!_dir!\.||>nul mkdir "Deutsche!_dir!") || (
     2>nul dir/ad/b .\Englishe!_dir!\.||>nul mkdir "Englishe!_dir!")

2>con: %__AppDir__%Timeout.exe /t -1|dir/a:d/tc/o-d/b *!_dir!. & endlocal


  • if() if else() else()您也可以通过一个循环来执行此操作for /f
@echo off

cd /d "%~dp0" && setlocal enabledelayedexpansion
set "_str=vers= lang= enhancements= modifications="

for /f usebackq^tokens^=1*delims^=^= %%a in (`%__AppDir__%Findstr.exe /ibl "!_str!" ^<.\conf.ini
=;`)do if /i "%%~a" == "vers" (if /i "%%~b" == "rem" (set "_sts=Remastered")else set "_sts=Original"
      )else if /i "%%~a" == "lang" (if /i "%%~b" == "eng" (set "_lng=EnglisheSprache")else set "_lng=DeutscheSprache"
      )else if /i "%%~a" == "enhancements" (if "%%~b" == "0" (set "_enh=EnhancementsOff")else set "_enh=EnhancementsOn" 
      )else if /i "%%~a" == "modifications" (if "%%~b" == "0" (set "_mod=ModificationsOff")else set "_mod=ModificationsOn")
     
2>nul mkdir "!_lng!.!_sts!.!_enh!.!_mod!" & endlocal


  • 对于所有 Cases.bat:
@echo off
cd /d "%~dp0" && setlocal enabledelayedexpansion
set "_str=vers= lang= enhancements= modifications="

for /f tokens^=1*delims^=^= %%a in ('%__AppDir__%Findstr.exe /ibl "!_str!" ^<.\conf.ini
=;')do if /i "%%~a" == "vers" (if /i "%%~b" == "rem" (set "_sts=Rem")else set "_sts=Orig"
      )else if /i "%%~a" == "enhancements" (if "%%~b" == "0" (set "_enh=0")else set "_enh=1" 
      )else if /i "%%~a" == "modifications" (if "%%~b" == "0" (set "_mod=0")else set "_mod=1"
      )else if /i "%%~a" == "lang" (if /i "%%~b" == "eng" (set "_lng=Eng")else set "_lng=Ger")

goto %:^)

 ::  Running: .\GerOrig00.bat | Case 01  ::
 ::  Running: .\GerOrig01.bat | Case 02  ::
 ::  Running: .\GerOrig10.bat | Case 03  ::
 ::  Running: .\GerOrig11.bat | Case 04  ::
 ::  Running: .\GerRem00.bat  | Case 05  ::
 ::  Running: .\GerRem01.bat  | Case 06  ::
 ::  Running: .\GerRem10.bat  | Case 07  ::
 ::  Running: .\EngRem11.bat  | Case 08  ::
 ::  Running: .\EngOrig00.bat | Case 09  ::
 ::  Running: .\EngOrig01.bat | Case 10  ::
 ::  Running: .\EngOrig10.bat | Case 11  ::
 ::  Running: .\EngOrig11.bat | Case 12  ::
 ::  Running: .\EngRem00.bat  | Case 13  ::
 ::  Running: .\EngRem01.bat  | Case 14  ::
 ::  Running: .\EngRem10.bat  | Case 15  ::
 ::  Running: .\EngRem11.bat  | Case 16  ::

%:^)
%__AppDir__%Findstr.exe /li ".\!_lng!!_sts!!_enh!!_mod!\.bat" <"%~f0"
Call .\!_lng!!_sts!!_enh!!_mod!.bat & endlocal & goto :eOf || exit /b
  • 文件conf.ini内容:
lang=deu
vers=rem
enhancements=0
modifications=1
  • 输出结果:
 ::  Running: .\GerRem01.bat  | Case 06  ::


  • 对全部dir\folder\Case选项:
@echo off

cd /d "%~dp0" && setlocal enabledelayedexpansion
set "_str=vers= lang= enhancements= modifications="

for /f usebackq^tokens^=1*delims^=^= %%a in (`%__AppDir__%Findstr.exe /ibl "!_str!" ^<.\conf.ini
=;`)do if /i "%%~a" == "vers" (if /i "%%~b" == "rem" (set "_sts=Remastered")else set "_sts=Original"
      )else if /i "%%~a" == "lang" (if /i "%%~b" == "eng" (set "_lng=EnglisheSprache")else set "_lng=DeutscheSprache"
      )else if /i "%%~a" == "enhancements" (if "%%~b" == "0" (set "_enh=EnhancementsOff")else set "_enh=EnhancementsOn" 
      )else if /i "%%~a" == "modifications" (if "%%~b" == "0" (set "_mod=ModificationsOff")else set "_mod=ModificationsOn")

goto %:^)

 :: Case N  | Strings   | Folder Names                                             
 :: -------   ---------   --------------------------------------------------------
 :: Case 01 | GerOrig00 | DeutscheSprache.Original.EnhancementsOff.ModificationsOff
 :: Case 02 | GerOrig01 | DeutscheSprache.Original.EnhancementsOff.ModificationsOn
 :: Case 03 | GerOrig10 | DeutscheSprache.Original.EnhancementsOn.ModificationsOff
 :: Case 04 | GerOrig11 | DeutscheSprache.Original.EnhancementsOn.ModificationsOn
 :: Case 05 | GerRem00  | DeutscheSprache.Remastered.EnhancementsOff.ModificationsOff
 :: Case 06 | GerRem01  | DeutscheSprache.Remastered.EnhancementsOff.ModificationsOn
 :: Case 07 | GerRem10  | DeutscheSprache.Remastered.EnhancementsOn.ModificationsOff
 :: Case 08 | EngRem11  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOn
 :: Case 09 | EngOrig00 | EnglisheSprache.Original.EnhancementsOff.ModificationsOff
 :: Case 10 | EngOrig01 | EnglisheSprache.Original.EnhancementsOff.ModificationsOn
 :: Case 11 | EngOrig10 | EnglisheSprache.Original.EnhancementsOn.ModificationsOff
 :: Case 12 | EngOrig11 | EnglisheSprache.Original.EnhancementsOn.ModificationsOn
 :: Case 13 | EngRem00  | EnglisheSprache.Remastered.EnhancementsOff.ModificationsOff
 :: Case 14 | EngRem01  | EnglisheSprache.Remastered.EnhancementsOff.ModificationsOn
 :: Case 15 | EngRem10  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOff
 :: Case 16 | EngRem11  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOn

%:^)
<con: %__AppDir__%Findstr.exe /i "|.!_lng!.!_sts!.!_enh!.!_mod!" <"%~f0"
2>nul md "!_lng!.!_sts!.!_enh!.!_mod!" & endlocal & goto :eOf || exit /b 
  • 文件conf.ini内容:
lang=eng
vers=ori
enhancements=1
modifications=0
  • 输出结果:
 :: Case 11 | EngOrig10 | EnglisheSprache.Original.EnhancementsOn.ModificationsOff


  • 对全部mkdir .\Folder还有你们的CaseStringOptions.bat一只蝙蝠:
@echo off 

cd /d "%~dp0" && setlocal enabledelayedexpansion
set "_str=vers= lang= enhancements= modifications="

for /f tokens^=1*delims^=^= %%a in ('%__AppDir__%Findstr.exe /ibl "!_str!" ^<.\conf.ini
=;')do if /i "%%~a" == "vers" (if /i "%%~b" == "rem" (set "_sts=Rem")else set "_sts=Orig"
      )else if /i "%%~a" == "enhancements" (if "%%~b" == "0" (set "_enh=0")else set "_enh=1" 
      )else if /i "%%~a" == "modifications" (if "%%~b" == "0" (set "_mod=0")else set "_mod=1"
      )else if /i "%%~a" == "lang" (if /i "%%~b" == "eng" (set "_lng=Eng")else set "_lng=Ger")

goto %:^)

 ::  Case N   | Strings/Script  | Folder Names To Create     
 ::  Case 01  | .\GerOrig00.bat | DeutscheSprache.Original.EnhancementsOff.ModificationsOff
 ::  Case 02  | .\GerOrig01.bat | DeutscheSprache.Original.EnhancementsOff.ModificationsOn
 ::  Case 03  | .\GerOrig10.bat | DeutscheSprache.Original.EnhancementsOn.ModificationsOff
 ::  Case 04  | .\GerOrig11.bat | DeutscheSprache.Original.EnhancementsOn.ModificationsOn
 ::  Case 05  | .\GerRem00.bat  | DeutscheSprache.Remastered.EnhancementsOff.ModificationsOff
 ::  Case 06  | .\GerRem01.bat  | DeutscheSprache.Remastered.EnhancementsOff.ModificationsOn
 ::  Case 07  | .\GerRem10.bat  | DeutscheSprache.Remastered.EnhancementsOn.ModificationsOff
 ::  Case 08  | .\EngRem11.bat  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOn
 ::  Case 09  | .\EngOrig00.bat | EnglisheSprache.Original.EnhancementsOff.ModificationsOff
 ::  Case 10  | .\EngOrig01.bat | EnglisheSprache.Original.EnhancementsOff.ModificationsOn
 ::  Case 11  | .\EngOrig10.bat | EnglisheSprache.Original.EnhancementsOn.ModificationsOff
 ::  Case 12  | .\EngOrig11.bat | EnglisheSprache.Original.EnhancementsOn.ModificationsOn
 ::  Case 13  | .\EngRem00.bat  | EnglisheSprache.Remastered.EnhancementsOff.ModificationsOff
 ::  Case 14  | .\EngRem01.bat  | EnglisheSprache.Remastered.EnhancementsOff.ModificationsOn
 ::  Case 15  | .\EngRem10.bat  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOff
 ::  Case 16  | .\EngRem11.bat  | EnglisheSprache.Remastered.EnhancementsOn.ModificationsOn

%:^)
for /f usebackq^tokens^=2-5^delims^=^|^  %%i in (`%__AppDir__%Findstr.exe /i "!_lng!!_sts!!_enh!!_mod!" ^<"%~f0"
`)do echo\%%i %%~j ^| File: !_lng!!_sts!!_enh!!_mod! ^| Dir: %%~l& 2>nul mkdir ".\%%~l" & Call "%%~k" & endlocal
  • 文件conf.ini内容:
lang=eng
vers=orig
enhancements=0
modifications=0
  • 输出结果:
Case 09 | File: EngOrig00 | Dir: EnglisheSprache.Original.EnhancementsOff.ModificationsOff

观察: usebackq^tokens^=2-5^delims^=^|^SpaceSpace%%i in (...

答案3

我找到了答案。非常感谢用户 Bobo 为我指明了正确的方向。这是代码,如果其他人也想做同样的事情:

@echo off

setlocal enabledelayedexpansion

for /f "tokens=2 delims==" %%G in ('findstr "lang=" conf.ini') do set LNG=%%G
for /f "tokens=2 delims==" %%G in ('findstr "vers=" conf.ini') do set VER=%%G
for /f "tokens=2 delims==" %%G in ('findstr "enhancements=" conf.ini') do set ENH=%%G
for /f "tokens=2 delims==" %%G in ('findstr "modifications=" conf.ini') do set MOD=%%G

if "%LNG%"=="deu" (
    if "%VER%"=="orig" (
        if "%ENH%"=="0" (
            if "%MOD%"=="0" (
                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir DeutscheSprache.Original.EnhancementsOff.modificationsOn
            )
        )
        if "%ENH%"=="1" (
            if "%MOD%"=="0" (
                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir DeutscheSprache.Original.EnhancementsOn.modificationsOn
            )
        )
    )
    if  "%VER%"=="rem" (
        if "%ENH%"=="0" (
            if "%MOD%"=="0" (
                mkdir DeutscheSprache.Remastered.EnhancementsOff.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir DeutscheSprache.Remastered.EnhancementsOff.modificationsOn
            )
        )
        if "%ENH%"=="1" (
            if "%MOD%"=="0" (
                mkdir DeutscheSprache.Remastered.EnhancementsOn.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir DeutscheSprache.Remastered.EnhancementsOn.modificationsOn
            )
        )
    )
)
if "%LNG%"=="eng" (
    if "%VER%"=="orig" (
        if "%ENH%"=="0" (
            if "%MOD%"=="0" (
                mkdir EnglischeSprache.Original.EnhancementsOff.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir EnglischeSprache.Original.EnhancementsOff.modificationsOn
            )
        )
        if "%ENH%"=="1" (
            if "%MOD%"=="0" (
                mkdir EnglischeSprache.Original.EnhancementsOn.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir EnglischeSprache.Original.EnhancementsOn.modificationsOn
            )
        )
    )
    if  "%VER%"=="rem" (
        if "%ENH%"=="0" (
            if "%MOD%"=="0" (
                mkdir EnglischeSprache.Remastered.EnhancementsOff.modificationsOff
            )
            if "%MOD%"=="0" (
                mkdir EnglischeSprache.Remastered.EnhancementsOff.modificationsOn
            )
        )
        if "%ENH%"=="1" (
            if "%MOD%"=="0" (
                mkdir EnglischeSprache.Remastered.EnhancementsOn.modificationsOff
            )
            if "%MOD%"=="1" (
                mkdir EnglischeSprache.Remastered.EnhancementsOn.modificationsOn
            )
        )
    )
)
endlocal

相关内容