Home > Net >  Choice script question when and how %1 can be used
Choice script question when and how %1 can be used

Time:09-07

Hi I saw some CHOICE examples using %1 at the end like

Choice /n /c 01234ABCD /m "Select Task Opton:"%1

but some did not have %1 at the end. Just curious when %1 should be used ? Appreciate any advice, please.

REVISED

Choice %~1 /n /c 01234ABCD /d 3 /t 20 %~2  " default is 3 after 20 secs " /m "Select Task Opton:%~2" 

if "%errorlevel%"=="3" goto three if "%errorlevel%"=="2" goto two

:three echo %errorlevel% echo This is Three goto next

enter image description here

CodePudding user response:

%1 is the first parameter provided to the batch file, so

thisbatch something somethingelse

would "set" %1 to something; %2 to somethingelse.

In the situation shown, it appends the parameter to the prompt-string, so

thisbatch /cs

would prompt with Select Task Opton:/cs.

BUT

If there is a Space between the closing quote and %1 then it could add extra options to the choice command, so

thisbatch /cs

would make the choices case-sensitive.

Better would be

Choice /n /c 01234ABCD /m "Select Task Opton:" %~1

where you could use

thisbatch "/d 3 /t 20"

to specify "default to choice '3' if no response in 20 seconds". The quotes on the thisbatch line causes the string between the quotes to be treated as a single string. The ~ in %~1 removes the enclosing quotes.

Personally, I'd use

Choice %~1 /n /c 01234ABCD /m "Select Task Opton:%~2"

where

thisbatch "/d 3 /t 20" " default is 3 after 20 secs "

would prompt with Select Task Opton: default is 3 after 20 secs

  • Related