Page 1 of 2
Passing batch argument to Choice command
Posted: 23 Apr 2016 18:55
by sambul35
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...
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 00:11
by ShadowThief
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
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 00:17
by sambul35
And if the value %opt% is empty, the Choice will accept typed input?
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 00:51
by ShadowThief
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
)
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 02:10
by thefeduke
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.
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 05:44
by sambul35
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?
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
Re: Passing batch argument to Choice command
Posted: 24 Apr 2016 09:39
by Aacini
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
Re: Passing batch argument to Choice command
Posted: 26 Apr 2016 15:52
by sambul35
Thanks, its a useful find.
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?
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 01:51
by pieh-ejdsch
thefeduke wrote:... because the choice character was displayed on the screen capitalized. Does anyone know why that would happen?
choice
/cs /c abc
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 02:19
by thefeduke
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.
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 05:49
by sambul35
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.
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 08:05
by sambul35
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....
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 10:27
by thefeduke
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.
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 10:57
by sambul35
@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
) 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.
Re: Passing batch argument to Choice command
Posted: 27 Apr 2016 11:24
by thefeduke
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.