Passing batch argument to Choice command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Passing batch argument to Choice command

#1 Post by sambul35 » 23 Apr 2016 18:55

I need to code a batch, where a variable value can either be entered as batch argument, or typed later as input of Choice command.

If an argument exists, can it be fed to Choice input instead of typing without saving that argument to a file? If doesn't exist, how to make the same Choice command accept a typed value?

Code: Select all

@echo off
if not "%1"=="" set "opt=%1"
choice /c stoez /n /m "Choose option" /t 15 /d z <%opt%
if errorlevel...

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Passing batch argument to Choice command

#2 Post by ShadowThief » 24 Apr 2016 00:11

You can pipe the value to choice instead of using a file.

Code: Select all

echo %opt%|choice /c stoez /n /m "Choose option" /t 15 /d z

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#3 Post by sambul35 » 24 Apr 2016 00:17

And if the value %opt% is empty, the Choice will accept typed input?

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Passing batch argument to Choice command

#4 Post by ShadowThief » 24 Apr 2016 00:51

You'd have to have a separate choice command for if %opt% is empty; otherwise, it's just going to beep a lot.

Code: Select all

if defined opt (
    echo %opt%|choice /c stoez /n /m "Choose option" /t 15 /d z
) else (
    choice /c stoez /n /m "Choose option" /t 15 /d z
)

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Passing batch argument to Choice command

#5 Post by thefeduke » 24 Apr 2016 02:10

This is much the same but organized weirdly. I had to bracket the first IF clause and redirect to nul, because the choice character was displayed on the screen capitalized. Does anyone know why that would happen?

Code: Select all

@echo off
SetLOCAL EnableDelayedExpansion
(if not "%1"=="" (
    Set "opt=%1"
    Echo.!opt!|choice /c stoez /n
    Set "Chose=!errorlevel!"
    Goto :Chosen
))>nul
choice /c stoez /m "Choose option " /t 15 /d z
Set "Chose=%errorlevel%"
:Chosen
IF %Chose% GEQ 5 (
    Echo.z was chosen or defaulted.
    GoTo :Xt
) Else (
    Echo.Process s,t,o or e values.
)
IF .%Chose% EQU .1 Echo.Do something for 's' choice
IF .%Chose% EQU .2 Echo.t was chosen
:XT
Exit /B

John A.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#6 Post by sambul35 » 24 Apr 2016 05:44

Thanks. The above options work allowing to separate errorlevel processing. But why Choice doesn't allow to type a letter if its piped an "empty" !opt! value, i.e. piped nothing?

Is it possible to similarly echo or pipe a list of batch arguments to a variable?

Code: Select all

echo %* | opt

One way I know is below, but may be there're non-iterative simple ways?

Code: Select all

set "var="
for %%a in (%*) do (set "var=!var! %%a")


Relevant "shorter code" question: when having a list of variable values, what's the elegant way to set multiple variables without repeated "set" statements? I know "set /A" allows to just list equations separated by commas, what about "set"?

Code: Select all

set "apl1=2" & set "apl2=" & set "apl3=kup" .............etc

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Passing batch argument to Choice command

#7 Post by Aacini » 24 Apr 2016 09:39

sambul35 wrote:Relevant "shorter code" question: when having a list of variable values, what's the elegant way to set multiple variables without repeated "set" statements? I know "set /A" allows to just list equations separated by commas, what about "set"?

Code: Select all

set "apl1=2" & set "apl2=" & set "apl3=kup" .............etc


You may use this method:

Code: Select all

set "vars=apl1=2,apl2=,apl3=kup,aplN=etc" 
set "%vars:,=" & set "%"


Antonio

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#8 Post by sambul35 » 26 Apr 2016 15:52

Thanks, its a useful find. :D

I need to start the same batch with different parameters using the same desktop shortcut. Can someone suggest, is it possible to manually enter a batch arguments in one line AFTER the batch is launched - from within it? What simple construct would allow that? Would it require to re-launch the batch with Call?

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Passing batch argument to Choice command

#9 Post by pieh-ejdsch » 27 Apr 2016 01:51

thefeduke wrote:... because the choice character was displayed on the screen capitalized. Does anyone know why that would happen?


choice /cs /c abc

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Passing batch argument to Choice command

#10 Post by thefeduke » 27 Apr 2016 02:19

pieh-ejdsch wrote:
thefeduke wrote:... because the choice character was displayed on the screen capitalized. Does anyone know why that would happen?


choice /cs /c abc
Thanks. I had not even noticed that the result was normally echoed back until your reply. I had no issue with the case. I'll just rephrase that I sent it to nul for silent running, but the why is no longer a mystery to me.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#11 Post by sambul35 » 27 Apr 2016 05:49

Since Choice output is processed by errorlevels, capitalization doesn't matter. I want to add that in my tests the construct wouldn't work if I try to add any other IF ELSE IF operators to it, apart from those 2 using Choice command, probably because the system was mixed up about calculating errorlevels. :wink:
Last edited by sambul35 on 28 Apr 2016 16:27, edited 1 time in total.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#12 Post by sambul35 » 27 Apr 2016 08:05

Here's one method to restart the same batch with various argument, using the same Windows desktop shortcut:

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "mes1=Do you want to enter batch arguments? Y/N"
set "mes2=Batch arguments:"
set "bat=M:\Packages\DamageControl.bat"

if "%1"=="" (
   choice /c yn /n /m "%mes1%" /t 10 /d n
   if !errorlevel! equ 1 (
      echo/ & set /p "args=%mes2% > "
      %bat% !args!)
) else (keep going....

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Passing batch argument to Choice command

#13 Post by thefeduke » 27 Apr 2016 10:27

sambul35,
It is premature to try to use !args! in the same statement where it is being set. Your ELSE completes the !errorlevel! IF, but the indentation implies completing the previous IF, so this example makes the decision more conscious:

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "mes1=Do you want to enter batch arguments? Y/N"
set "mes2=Batch arguments:"
 
if "%1"=="" (
   choice /c yn /n /m "%mes1%" /t 10 /d n
   if !errorlevel! equ 1 (
      echo/ & set /p "args=%mes2%" &echo\
      Echo.M:\Packages\DamageControl.bat !args!
   ) else Echo(Going nowhere....
) else Echo(keep going....

John A.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Passing batch argument to Choice command

#14 Post by sambul35 » 27 Apr 2016 10:57

@thefeduke

I post only snippets that work for me (in Win10), unless there's a link to source. They might not work in other Windows versions, as I have no access to test. If you tested (!!!) and found my above snippet not working, pls indicate Windows version, and the error(level :P ) printed. Any feedback is always appreciated.

Btw, ELSE in my above example completes 1st IF level, not ERRORLEVEL, as the indentation correctly implies. The ERRORLEVEL IF statement doesn't need ELSE here. :mrgreen:
Last edited by sambul35 on 28 Apr 2016 16:28, edited 2 times in total.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Passing batch argument to Choice command

#15 Post by thefeduke » 27 Apr 2016 11:24

Code: Select all

C:\Users\Zani\Scripts>args2orig go
C:\Users\Zani\Scripts>args2orig
C:\Users\Zani\Scripts>args2orig

C:\Users\Zani\Scripts>setlocal EnableExtensions EnableDelayedExpansion

C:\Users\Zani\Scripts>set "mes1=Do you want to enter batch arguments? Y/N"

C:\Users\Zani\Scripts>set "mes2=Batch arguments:"

C:\Users\Zani\Scripts>set "bat=M:\Packages\DamageControl.bat"
C:\Users\Zani\Scripts>C:\Users\Zani\Scripts>cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
That is the output of a fresh cut-and-paste on Windows 7 (64bit). The first is with argument 'go', the second with no arguments and the last with Echo ON.
John A.

Post Reply