Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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]

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.

CHOICE.COM from DOS works and it's only 2KB. LOL
=====================================
If at first you don't succeed, you're about average.M2

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.

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.

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.

Hi klint,
You're right; CHOICE.COM trashes a LFN prompt.
=====================================
If at first you don't succeed, you're about average.M2

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.

![]() |
Search a file name, Batch
|
set command and xcopy
|

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