Reputation: 47
I have an issue with batch file as you can see. I have tried searching for hours but, I did not find anything related to my question. So here is my code (English is not my first language so, please bear with me):
:fileexist
For %%f in ("bin/*.exe") do (
set /A count+=1
set c!count!=%%f
)
Set "input="
Set /P input= Select
If "!input!" GTR "!count!" (Goto :fileexist)
If "!input!" EQU "0" (Set Exe=No executable file) & (Goto :nofileexist)
If "!input!" LSS "1" (Goto :fileexist)
If "!input!" LEQ "!count!" (Set Exe=!c%input%!) & (Goto :gotfile)
Goto :fileexist
Now i have three (3) executable files in that folder (bin) by default which is just an example as the client would have more than 3 or maybe less. Now let go with the example, The code works fine if i input correctly (1, 2 or 3) and selects the file but, if i input (12, 13, 14..) it still selects the 1st file note that this works with any number as long as the first digit is "1".
Similarly if i input (21, 22, 23..and so on) it selects the 2nd file and same with this, it will work with any number as long as the first digit is "2" but, now comes the "3" and guess what? It will not accept any higher number than that whether its (31, 32 ,4000..or so..) any number above "3" is not accepted and that's what i want but, I also want it to not accept other digits like the (12, 1400, 23, 27553.. etc).
I spent hours trying to figure it out but no luck that's why this is my last hope which involves the following:
I am sorry for the wall of text and for my English, of course. Please let me know if something is confusing and thank you for your time looking into this. It would be much appreciated if we all could figure out the root of this cause and came up with a solution. Thank you.
Upvotes: 3
Views: 128
Reputation: 49127
Here is a batch code based on your batch code with lots of verifications on entered user input.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
:RunAgain
set "Count=0"
for %%F in ("bin\*.exe") do (
set /A Count+=1
set "File!count!=%%~fF"
)
:EnterNumber
rem Define as default value a double quote.
set "Input=""
rem Ask user for entering a number.
set /P "Input=Enter number between 0 and %Count%: "
rem Remove double quotes if entered by user if entered anything at all.
rem This removal of all double quotes in entered string would result in
rem a syntax error if default value for Input is not a double quote and
rem the batch user hits just RETURN or ENTER without entering anything.
set "Input=%Input:"=%"
rem Let user enter the number again if nothing was entered by the user.
if not defined Input goto EnterNumber
rem Check if entered string consists of only digits, i.e. is a positive number.
set "NoneDigit="
for /F "delims=0123456789" %%N in ("%Input%") do set "NoneDigit=1"
rem Let user enter the number again if entered string was not a positive number.
if defined NoneDigit goto EnterNumber
rem Remove leading zeros to avoid number being interpreted as octal number.
set "Number="
for /f "tokens=* delims=0" %%N in ("%Input%") do set "Number=%%N"
rem Is the entered number 0?
if not defined Number (
set "Executable=No executable file"
goto NoExecutable
)
rem Has the user entered a too large number.
if %Number% GTR %Count% goto EnterNumber
set "Executable=!File%Number%!"
echo Selected EXE is: %Executable%
rem Do whatever should be done with this executable.
goto RunAgain
:NoExecutable
echo No executable selected.
:ExitBatch
endlocal
The comment lines starting with command rem (yes, it is a command) should explain the important blocks.
The loop for removing leading zeros was taken from answers on Remove leading zeros in batch file.
For more details on this batch code open a command prompt window, enter the following commands, and read the help output for each command.
for /?
goto /?
if /?
set /?
setlocal /?
Upvotes: 2
Reputation: 14325
Batch variables are treated like strings unless they're explicitly treated like numbers. In this case, the quotes in your if
statements are forcing the variables to be treated like strings since quotes aren't integers. Because of this, a string comparison is being done where the variables are being compared one character at a time, which means that 12 comes before 2 since 1 is less than 2.
If !input! GTR !count! (Goto :fileexist)
If !input! EQU 0 (Set Exe=No executable file) & (Goto :nofileexist)
If !input! LSS 1 (Goto :fileexist)
If !input! LEQ !count! (Set Exe=!c%input%!) & (Goto :gotfile)
Upvotes: 1