PAB wrote: ↑07 Aug 2020 13:58
Code: Select all
<snip>
Echo OS Index number is "OS_Index" & Pause
So would the code above work and [
a] ignore just pressing <Enter>, [
b] ignore inputting a zero, [
c] ignore a number greater than 12, [d] ignore ANY letter?
Would I use the number held in
OS_Index by using
"OS_Index" or something else please?
I noticed that with your code you don't use any
" or
!.
I haven't got access to my computer until Monday but I will then be able to test out any new code or recommendations.
Thanks in advance.
The problem with using
Set /P is that your end user can enter nothing at all or anything they like.
In my implementation, it doesn't matter what they enter, the code will continue asking until they type an integer value of at least
1 and not more than
12, before pressing their
[ENTER] key.
There is no need to delay expansion, and the end user can, if they wish, enter
^ & > < % ! " | \ ? / etc, without it exiting/breaking the script.
You do not modify any of the code, only the last line which will be your own code, everything else,
(especially the input line which specifies the range of entries allowed), remains exactly as it is.
To use my code this is what I'd expect, (long line version):
Code: Select all
:Menu
Set "OS_Index="
Set /P "OS_Index=Input Index Number [1..12]>"
If Not Defined OS_Index (GoTo Menu)Else Set OS_Index|"%__AppDir__%findstr.exe" "^OS_Index=[123456789]$ ^OS_Index=1[012]$">NUL||GoTo Menu
Echo You entered %OS_Index% as your OS Index number. & Pause
and, (split line version):
Code: Select all
:Menu
Set "OS_Index="
Set /P "OS_Index=Input Index Number [1..12]>"
If Not Defined OS_Index (GoTo Menu)Else (
Set OS_Index | "%__AppDir__%findstr.exe" ^
"^OS_Index=[123456789]$ ^OS_Index=[1][012]$" 1> NUL ^
|| GoTo Menu)
Echo You entered %OS_Index% as your OS Index number. & Pause
The Index number entered by the end user will be held in the variable named OS_Index, the value of which will be
%OS_Index%,
(as displayed on the example last line).