Computing.Net > Forums > Programming > How to set a default choice in bat?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to set a default choice in bat?

Reply to Message Icon

Name: yuenli
Date: October 6, 2008 at 22:12:43 Pacific
OS: XP
CPU/Ram: Intel P4, 1Gb
Product: Dell
Comment:

Hi I had write a batch file coding to ask user to enter y or n to quit the batch program. Is there any way to set the default choice to y means after running the batch coding it will automatically quit it without waiting for user input? I try to add "/ty,1" in it but it seem does not work. Thanks.

set /p choice=Are you sure you want to quit. [y/n]



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 7, 2008 at 02:37:01 Pacific
Reply:

The easy way to do this is to get choice.exe, choice.com also works but it will be better to use choice.exe on winXP.

You can get the file youself, or can follow the steps outlined below.

I also recommended the below steps for any one using choice.exe from the nt4 resource kit as the file obtained has a newer time stamp(Saturday, 26 April 2003) and larger file size (88,576 bytes). [edit]By this of course I mean bug fixes.


Microsoft don't seem to have choice.exe available for download on it's own, so you can either download the whole nt resource kit or another file that contains choice.exe and extract it.

I suggest using one of the Microsoft Platform Support Reporting Utilities. Which is available for download here links to microsoft seem to have a habit of breaking, so if the link doesn't work go to microsoft.com and search for "816819" (without quotes).

Once to this webpage you have a choice(how topical) of four files that contain choice.exe, any one of them will do as they all contain choice.exe and they are all under 1mb. Some of the files available for download on this page don't contain choice.exe, the ones that do are mpsrpt_cluster.exe, mpsrpt_network.exe, mpsrpt_setupperf.exe and mpsrpt_sus.exe. They are found under the headings "Clustering", "Networking", "Base/Setup/Storage/Print/Performance" and "Software Update Services"

Once you have one of these files DO NOT DOUBLE CLICK ON IT this will install the utilities, we don't want this. Go to 7-Zip's website and download 7 zip, other archivers will work just as well, I'm using it here because it is small and free. Install 7-zip. Now right click on the file and there should be an item 7-zip, goto it and select "open archive". Find choice.exe in the list, right click on it and select "copy to" choose a location, click ok and your in business you have choice.exe you can use in you batch file.

Now simply put choice.exe either in the same folder as your batch or somewhere included in the path enviroment variable, like what ever %windir% expands to.

Using choice.exe is simple, the folowing command will choose y automatically after 30 seconds:


choice /c:yn /n /t:y,30 "Are you sure you want to quit. [y/n]"

The option chosen will set an errorlevel according to it's position after /c: e.g. the choice y above will set an errorlevel of 1 and n will set it to 2 and so on... Always start from the highest errorlevel and work your way down, like so:

if %errorlevel%==2 do what ever n does
if %errorlevel%==1 do what ever y does

See choice /? for more info.

0

Response Number 2
Name: Mechanix2Go
Date: October 7, 2008 at 02:46:27 Pacific
Reply:

CHOICE.COM from DOS works and it's only 2KB. LOL


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: Judago
Date: October 7, 2008 at 03:34:22 Pacific
Reply:

I read choice.com had some problems in a multitasking enviroment, apparently that's the reason it wasn't included in NT. I play it safe and use choice.exe, though I must say choice.com from windows 98 didn't give me any problems under xp. Makes me wonder what the other 86kb are......

[edit]
Choice.exe is a 32bit executable and doesn't invoke ntvdm, meaning ntvdm isn't running in the background until the instance of cmd that invoked it is terminated.


0

Response Number 4
Name: klint
Date: October 7, 2008 at 04:27:55 Pacific
Reply:

Yes, choice.com works, but I find that when I run DOS utilities when my current directory contains long names or spaces (such as C:\Document and Settings\etc...) and/or my Command Prompt window has custom width/colours, then after exiting from the DOS my CD ends up with short names and my window end up as 80 columns, white-on-black, which is not very nice. Running a program shouldn't have such drastic effects. So I would prefer choice.exe for Win32 instead.


0

Response Number 5
Name: Judago
Date: October 7, 2008 at 14:19:24 Pacific
Reply:

I just found another very good reason to use choice.exe over choice.com. It can wait longer that 99 seconds, it seems a whole lot longer, maybe even hours, though I didn't have time to wait around and check it's output to be sure.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: October 7, 2008 at 17:50:29 Pacific
Reply:

Hi klint,

You're right; CHOICE.COM trashes a LFN prompt.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: TanelTM
Date: October 13, 2008 at 06:18:28 Pacific
Reply:

You can't make the set /p command to time out but you can set it so when the user just presses "enter" it will be the default choice.
for example you have 3 choices and the first choice is the default choice...

set q=R
set /p q=Here are your choices, [R]etry, [A]bort, [F]ail. Default is retry.
...
...

so the user can enter either R, A or F and also if he doesn't choose anything, just presses "enter" the script will choose the "R" option because it was the previous value for the "q" variable.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Search a file name, Batch set command and xcopy



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to set a default choice in bat?

Auto press enter www.computing.net/answers/programming/auto-press-enter/17510.html

how to compile a .Reg file in VC++? www.computing.net/answers/programming/how-to-compile-a-reg-file-in-vc/10251.html

How to register a .NET dll www.computing.net/answers/programming/how-to-register-a-net-dll/5440.html