The problem: Windows "choice" command, which can be used for getting one character input from user (e.g. "Are you sure [y/n]", "Choose item (1, 2, 3, 4)" etc.) returns its output as errorlevel, where value of errorlevel is equal to index of choosen character. For choices "yni", choosing "n" will return errorlevel 2, for "i" it will be "3" etc. That could make script code harder to write, read and modify. Also, for compatibility reasons, errorlevels should be checked from the biggest to the smallest. One may encounter unsuspected behavior when e.g. the result will be errorlevel 3 and conditional commands return their own errorlevel, let's say 2. In such case two conditional blocks will be executed. Complicated enough? The last straw: such behavior heavily depends on Windows version, so it may be really unexpected.
Functions: My scripts offers two functions (the latter is optional):
- Getting choice result not as an errorlevel variable, but as character given by user. Some tricks ensure that changing possible choices is just a matter of modyfing single variable. No adding more lines with every next choice!
- Saving last choice for reusing it in the next run of the script as default value. The saved choice can be also reused by external tools, scripts and in other ways.
Files: a) getChoiceCharacter.bat - get choice result as character, not number b) setLastChoiceAsDefault.bat - use choosen value as default on the next run c) setLastChoiceAsDefaultVer2.bat - same as above, but first run haven't predefined value
Licensing: (C) krzysiu.net 2016. The work is released as MIT, LGPL, GPL and CC BY 4.0 licenses. You may choose one of them. In very special cases, you may use it as public domain - I trust you can make correct... choice :) If you enjoy my script and you want to support me, you can donate me by PayPal: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVJ35VQGHAH6J If you liked that code, used it anywhere and you find it good, I'll be happy to hear from you.