批处理文件输入错误?

3

我有一个批处理文件的问题,如下所示。我已经搜索了几个小时,但是没有找到与我的问题相关的内容。因此,这是我的代码(英语不是我的母语,请谅解):

: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

现在,在该文件夹(bin)中默认有三个可执行文件(示例),但实际客户端可能少于3个或多于3个。 让我们以示例为例,如果我正确输入(1、2或3)并选择文件,则代码可以正常工作。 但是,如果我输入(12、13、14等),它仍然会选择第一个文件,请注意,只要第一个数字是“1”,则任意数字都可以正常工作。
同样,如果我输入(21、22、23等),它将选择第二个文件,并且任何以“2”开头的数字都可以正常工作,但是现在出现了“3”,你猜怎么着? 它将不接受任何比“3”更高的数字,无论是(31、32、4000左右),任何大于“3”的数字都不被接受,我也希望它不接受其他数字,例如(12、1400、23、27553等)。
我花了几个小时试图解决它,但没有任何运气,这就是为什么这是我的最后一次尝试,其中涉及以下内容:
- 将延迟扩展从“!input!”替换为“%input%”。 - 替换大于/小于/等于符号。 - 在循环内设置set命令周围的引号。 - 尝试使用类似于此%count:-%的方法删除set命令创建的隐藏空格(即使在引号周围),还尝试了更多方法,但没有可能运气好。
对于我的英语和这堵文字墙,我很抱歉。 如果有什么不清楚的地方,请告诉我,非常感谢您花时间研究此事。 如果我们都能找出问题根源并想出解决方案,那就太好了。 谢谢。

尝试在if语句中删除引号。我正在用手机发布,所以无法测试,但我认为它们会使代码执行字符串比较而不是数字比较。 - SomethingDark
@SomethingDark:非常感谢您的快速响应和修复。对于我的先前回复,有点误解(我以为您说要在帖子中删除引号)。是我的错误。但是在重新阅读您的帖子后,我意识到您的意思是从实际代码中删除引号,所以我这样做了,然后就可以了。非常感谢您,先生。谢谢。 - Anonymous
在这里添加“(已解决)”来编辑您的标题是不合适的。如果您收到的答案解决了您的问题,请接受它。如果您以其他方式找到了解决方案,请在下面的适当位置添加自己的答案并分享解决方案,以便其他人也可以从中受益。在主题中添加“(已解决)”(或在问题本身中进行编辑以添加解决方案)不是正确的做法。 - Ken White
@Ken White:我真诚地为此道歉。我会确保这种情况不再发生。正如您可能已经看到的那样,我从用户(SomethingDark)那里得到了解决方案。我不知道如何接受它作为答案,也许您可以告诉我? - Anonymous
不用道歉,我只是在解释。:-) 你不能把@SomethingDark的评论作为答案接受;希望SomethingDark会回来并将其变成答案。如果一段时间后没有人这样做,你可以自己添加一个答案,并提到你从哪里得到它(在上面的评论中)。在这里回答自己的问题是完全可以接受的-有关更多信息,请参见Can I answer my own question? - Ken White
显示剩余3条评论
2个回答

2
这是一个基于您的批处理代码的批处理代码,对输入的用户输入进行了大量验证。
@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

rem命令开头的注释行(是的,它是一个命令)应该解释重要模块。
去除前导零的循环取自Remove leading zeros in batch file中的答案。
有关此批处理代码的更多详细信息,请打开命令提示窗口,输入以下命令,并阅读每个命令的帮助输出。
  1. for /?
  2. goto /?
  3. if /?
  4. set /?
  5. setlocal /?

我非常感谢你提供的反馈(社区的大力支持)。今天我学到了新东西,这要归功于你(每个命令的解释都很好)。我可以清楚地看到你在代码中付出了很多努力,但是正如你所看到的,我已经有自己的代码在运行,并且它完全按照我的意愿运行得很好。虽然我会给你的答案点赞(在声望15分),因为我确实觉得它非常有用。再次感谢您,先生,我很抱歉没有使用您的代码,但我确实学到了新东西,这是一个加分项。 - Anonymous

1
批处理变量被视为字符串,除非它们明确被视为数字。在这种情况下,您 if 语句中的引号会强制将变量视为字符串,因为引号不是整数。因此,进行了字符串比较,其中变量逐个字符进行比较,这意味着12在2之前,因为1小于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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接