1

I made a simple batch file to execute a executable via some options which are provided upon launch.

Some thing like this :

: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%

So basically this works. But what i need to have is a way to include another launch parameter for my program if both "Option 1" and "Option A" are selected. Not in "Option 2" and "Option B".

so that my launch look like this

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

Edit : i've made some mistakes here on this command line but i won't correct it since @avery_larry pointed it out.

I'm sorry if i made this confusing, please let me know if need to clarify or elaborate further. :)

PRince4
  • 37
  • 8

3 Answers3

1

untested

If a variable is not set to anything, then it will expand to nothing. Setup your 3rd variable and make sure it's not set if the conditions aren't met. Something like this:

set if1_a=
if "%set1%"=="1" (
   if "%set2%"=="A" (
      set "if1_a=-option"
   )
)

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

Note that I made the hyphen part of the variable. Also it's probably supposed to be:

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

and finally you probably want to use if /i to make it case insensitive.

avery_larry
  • 2,069
  • 1
  • 5
  • 17
  • Yes! Thank you it worked. I just needed to put the starting quote of set "if1_a=-option" to set if1_a="-option" because of spaces in file names, that quote took me two more minutes to reply you. And yes i realized my mistake after the moment i was about to see your reply. I actually changed the names prior submitting but forgot to change the last ones. Thanks again! – PRince4 Apr 18 '20 at 14:48
  • 1
    avery_larry, might I offer a much simpler looking solution? `If /I "%set1%%set2%" == "1A" Set "if1_A=-"option""`. Then, just in case the possible allowable inputs change, outside of the question posed, perhaps `program.exe -"%set1%" -"%set2%" %if1_A%`, _doublequoting the options for safety_, would be advisable. – Compo Apr 18 '20 at 16:52
0

Problems with user input can be avoided through the use of an input validation function, as exampled here:

@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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
0

Whilst your question may have been using the input options just for the purposes of the question, and not real world ones; as they are known options, I've decided to provide a solution which uses the more robust built-in choice.exe command:

@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
Compo
  • 36,585
  • 5
  • 27
  • 39