【问题标题】:Include another option/file While selecting specific Options在选择特定选项时包含另一个选项/文件
【发布时间】:2020-08-01 01:55:06
【问题描述】:

我制作了一个简单的批处理文件,通过启动时提供的一些选项来执行可执行文件。

类似这样的事情:

:A
Echo Option 1
Echo Option 2
Set /p set1=Choice :
if %set1%==1 set A=Set1_1
if %set1%==2 set A=Set1_2
goto Set_2

:B
Echo Option A
Echo Option B
Set /p set2=Choice :
if %set2%==A set B=Set2_A
if %set2%==B set B=Set2_B
goto launch

:launch
program.exe -%A% -%B%

所以基本上这行得通。但是,如果同时选择了“选项 1”和“选项 A”,我需要一种方法来为我的程序包含另一个启动参数。不在“选项 2”和“选项 B”中。

让我的发布看起来像这样

program.exe -%set1% -%set2% -%if1_A%

编辑:我在这个命令行上犯了一些错误,但我不会更正它,因为@avery_larry 指出了它。

很抱歉,如果我让这变得混乱,请让我知道是否需要进一步澄清或详细说明。 :)

【问题讨论】:

    标签: batch-file cmd command


    【解决方案1】:

    未经测试

    如果一个变量没有被设置为任何值,那么它将扩展为空。设置你的第三个变量,如果条件不满足,确保它没有被设置。像这样的:

    set if1_a=
    if "%set1%"=="1" (
       if "%set2%"=="A" (
          set "if1_a=-option"
       )
    )
    
    program.exe -%set1% -%set2% %if1_A%
    

    请注意,我将连字符作为变量的一部分。也应该是:

    program.exe -%A% -%B% %if1_A%
    

    最后你可能想使用if /i 使其不区分大小写。

    【讨论】:

    • 是的!谢谢你成功了。由于文件名中有空格,我只需要将 set "if1_a=-option" 的起始引号设置为 set if1_a="-option" ,该引号花了我两分钟的时间才回复您。是的,在我即将看到你的回复的那一刻,我意识到了我的错误。我实际上在提交之前更改了名称,但忘记更改最后一个。再次感谢!
    • avery_larry,我可以提供一个看起来更简单的解决方案吗? If /I "%set1%%set2%" == "1A" Set "if1_A=-"option""。然后,以防万一可能允许的输入发生变化,在提出的问题之外,也许program.exe -"%set1%" -"%set2%" %if1_A%双重引用安全选项是可取的。
    【解决方案2】:

    可以通过使用输入验证函数来避免用户输入问题,如下所示:

    @Echo off & CD "%~dp0"
    TITLE %~n0 & Setlocal
    
    ::: / Creates variable /AE = Ascii-27 escape code.
    ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
    ::: - https://stackoverflow.com/a/34923514/12343998
    :::
    ::: - /AE can be used  with and without DelayedExpansion.
        Setlocal
        For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
            Endlocal
            Set "/AE=%%a"
        )
    ::: \
    
    %= Remark =%
    
        (Set LF=^
    
    
    %= NewLine variable to split set /p input line =%)
    
    %= create blank batch file to store and retrieve variables in for automatic reload =%
    
        If not Exist "inputlog.bat" (Echo(@Echo Off>inputlog.bat & Goto :main)
        For %%A in ( "%/AE%[36m[%/AE%[34mN%/AE%[36m]ew" , "[%/AE%[34mL%/AE%[36m]oad%/AE%[0m" )Do Echo.%%~A
        Choice /N /C nl >nul
        If errorlevel 2 (Goto :load)    
        Goto :Main
    
    :Input <VarName> <Optional - define valid values with each additional parameter>
        Set "variable=%~1"
        Setlocal EnableDelayedExpansion
        IF not "%~2"=="" For %%A in (%*) Do (If not "%%~A"=="%1" (Set "valid=%%~A !valid!"))
    
    :Confirm
    %= allow input of variable, test input, store for reloading. =%
    
        Echo(%/AE%[35m
        Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
        IF "!input!"=="" (
            Echo(%/AE%[31m!variable! required.
            Goto :Confirm
        )
        IF "%~2"=="" (
            ECHO(Set "!variable!=!input!">>inputlog.bat
            Endlocal & Set "%variable%=%input%"
            Exit /B
        )
    
        For %%A in (!valid!) Do (
            If /I "!input!"=="%%~A" (
                    ECHO(Set "!variable!=!input!">>inputlog.bat
                    Endlocal & Set "%variable%=%input%"
                    Exit /B
            )
        )
        Echo(%/AE%[31m!variable! required.
        Echo(%/AE%[36mSelect from:
        For %%A in (!valid!) Do (Echo(%/AE%[33m%%~A%/AE%[0m)
        Goto :Confirm
    
    :main
    %= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
        Call :Input language english spanish
    
    %= Define multiple variables at once =%
        For %%A in ("name" "age" "location" "job" "hobbies") Do (Call :Input %%A)
    
    %= undefine local variables =%
        endlocal
    %= Display that variable definitions have been removed =%
        For %%A in ("language" "name" "age" "location" "job" "hobbies") Do Set %%A
    
    :load
    %= Reload the variables from the input Log and display. =%
        Call inputlog.bat
    %= Demonstrate that variables have been reloaded =%
        Setlocal EnableDelayedExpansion
        For %%A in ("language" "name" "age" "location" "job" "hobbies") Do (Echo(%/AE%[37m%%~A:!LF!%/AE%[36m%/AE%[33m!%%~A!%/AE%[32m%/AE%[0m)
        pause
    

    【讨论】:

      【解决方案3】:

      虽然您的问题可能只是出于问题的目的使用输入选项,而不是现实世界的;由于它们是已知选项,因此我决定提供一个使用更强大的内置 choice.exe 命令的解决方案:

      @Rem Undefine any existing variables to be used.
      @For /F "Delims==" %%G In ('Set Opt[ 2^>NUL')Do @Set "%%G="
      
      @Rem Request first argument.
      @"%__APPDIR__%choice.exe" /C 12 /M "First argument"
      
      @Rem Define the first argument with the value chosen.
      @Set "Opt[1]=%ERRORLEVEL%"
      
      @Rem Request second argument.
      @"%__APPDIR__%choice.exe" /C AB /M "Second argument"
      
      @Rem Define the second argument with the value chosen.
      @If ErrorLevel 2 (Set "Opt[2]=B")Else (Set "Opt[2]=A"
          Rem Define third argument if first chosen was 1
          If %Opt[1]% Equ 1 Set "Opt[3]=-"Third option string"")
      
      @Rem Launch program with required options.
      @Echo program.exe -"%Opt[1]%" -"%Opt[2]%" %Opt[3]%
      
      @Rem Prevent cmd window from possibly closing early.
      @Pause
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-28
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 2018-06-29
        • 1970-01-01
        相关资源
        最近更新 更多