【问题标题】:Batch, I'm having a little findstr trouble批处理,我遇到了一些 findstr 麻烦
【发布时间】:2014-11-10 18:58:40
【问题描述】:

所以我编写了一个批处理脚本,它可以帮助组织我在游戏中运行的一个小彩票。 它要求用户输入玩家姓名,然后要求输入 1-100 的最多 5 个彩票号码。 该脚本将玩家姓名和他们选择的号码添加到文本文档中,并将这些号码添加到文本文档中,以便它知道哪些号码已经“被占用”,如果我尝试输入重复(还进行了一些排序,以便“按顺序”显示“采用”的数字,以便于查看)。

当我使用“findstr /c:”查看“获取的数字”文件时,如果我查找“1”,它会假设 1、10、100 等是相同的数字,因为...这些数字有我在他们身上寻找什么。我通过输入单个数字 01-09 解决了这个问题,但这仍然让我留下“10”和 100(在 findstr 眼中)相同。

所以我尝试使用“findstr /x /c:”来获得确切的值,这就是我遇到问题的地方。现在 /x 在那里,它只是忽略检查值是否相同,我不明白为什么,请帮助:P 它让我发疯。

@echo off
echo --------------------
echo Lottery Program
echo --------------------
echo.
echo.
echo These are the Taken numbers.
type takennumbersnice.txt
echo.
echo.
set /p name= Please type the players name 
:thestart
echo ---------------------------
set /p multinum1= Please type their first number [01-100] 
findstr /x /c:%multinum1% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart
) || (
echo %multinum1%>> takennumbers.txt & goto next) 
:next
:thestart2
echo ---------------------------
set /p multinum2= Now type their second number [0 if they only picked 1 number] 
if '%multinum2%'=='0' (
goto endofnums
) else (
goto 3
)
:3
findstr /x /c:%multinum2% takennumbersnice.txt && (
echo This number has already been taken! & pause  && goto thestart2
) || (
echo %multinum2%>> takennumbers.txt &goto next2)
:next2
:thestart3
echo ---------------------------
set /p multinum3= Now type their third number [0 if they only picked 2 numbers] 
if '%multinum3%'=='0' (
goto endofnums
) else (
goto 4
)
:4
findstr /x /c:%multinum3% takennumbersnice.txt && (
echo This number has already been taken! & pause  && goto thestart3
) || (
echo %multinum3%>> takennumbers.txt &goto next3)
:next3
:thestart4
echo ---------------------------
set /p multinum4= Now type their fourth number [0 if they only picked 3 numbers] 
if '%multinum4%'=='0' (
goto endofnums
) else (
goto 5
)
:5
findstr /x /c:%multinum4% takennumbersnice.txt && (
echo This number has already been taken! & pause  && goto thestart4
) || (
echo %multinum4%>> takennumbers.txt &goto next4)
:next4
:thestart5
echo ---------------------------
set /p multinum5= Now type their fifth number [0 if they only picked 4 numbers] 
if '%multinum5%'=='0' (
goto endofnums
) else (
goto 6
)
:6
echo ---------------------------
findstr /x /c:%multinum5% takennumbersnice.txt && (
echo This number has already been taken! & pause  && goto thestart5
) || (
echo %multinum5%>> takennumbers.txt)
:endofnums
set number= %multinum1% %multinum2% %multinum3% %multinum4% %multinum5%
echo %name% has chosen the number[s] %number%
echo %name% %number% >> lottery.txt
sort < takennumbers.txt > takennumbersnice.txt
echo press any key to exit
pause > nul
exit

【问题讨论】:

    标签: batch-file findstr


    【解决方案1】:

    首先,findstr 足够聪明,可以分辨出 1、10 和 100 之间的差异,而无需格式化小于 10 的数字。我建议删除前导 0,因为这就是批处理指示数字在其中的方式十六进制,你会得到 08 和 09 的错误。

    那么,你的问题给我带来了一些麻烦,但我想通了。当您将数字回显到文本文件时,您无意中在数字末尾添加了一个空格,当 findstr 查找确切的字符串时,它没有找到它,因为不是“15”,而是“15” (注意空格)。

    谢天谢地,解决方案很简单。改变

    echo %multinum1%&gt;&gt;takennumbers.txt &amp; goto next)

    echo %multinum1%&gt;&gt;takennumbers.txt&amp;goto next)

    然后对其他三个类似的行执行此操作。

    【讨论】:

    • 嗨,你能粘贴固定代码吗:P 我做了你所说的改变,但现在如果“第二人”选择一个数字,它会回显为“回声关闭”而不是数字出于某种原因
    • 除了您最初提到的之外,我还没有测试过任何意外行为。让我看看……
    • setlocal enabledelayedexpansion行添加到@echo off下的文件顶部,并将echo %multinum1%&gt;&gt;takennumbers.txt&amp;goto next更改为echo !multinum1!&gt;&gt;takennumbers.txt&amp;goto next
    • 非常感谢!你修好了 :) 我不明白为什么要修它,但确实如此。
    • 我自己并不完全确定,但根据它如何解决与 for 循环类似的问题,我想这与批处理并不总是与括号配合得很好。
    猜你喜欢
    • 2019-10-21
    • 2013-01-06
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多