如果我不在srch字符串中的查询之间使用空格,下面的批处理文件完美工作。一旦我添加一个空间,程序跳出并退出。Findstr不喜欢变量输入中的空格。
@echo off
mode con:cols=90 lines=40
:top
echo *******PHONE BOOK******
echo Press Q to quit
echo.
set /P srch=Enter Search query:
IF /i %srch% EQU Q goto :end
findstr /i %srch% %USERPROFILE%\documents\names.txt
IF %ERRORLEVEL% EQU 1 goto :e1
pause
cls
goto top
pause
:end
set /p wate=Press any key to Quit
exit
:e1
echo No match found edit names.txt in your documents folder.
pause
cls
goto top
exit
另存为PhoneQuery.bat
另存为以下在文档中的文件夹 “names.txt中”。
Name Address Local Phone#
Bob Billings 123 here ST St Paul MI 800-555-5555
Information nil any 411
Fire Dept Multi Dubai +1-992-611-1212
'FINDSTR /我 “%SRCH%” %USERPROFILE %\ documents \ names.txt' – Squashman
帮助文件显示了一些很好的例子:**使用空格分隔多个搜索字符串,除非argume nt前缀为/ C。例如,'FINDSTR'hello there“x.y”在文件x.y中搜索“hello”或“there”。 'FINDSTR/C:'hello there“x.y”在文件x.y中搜索“hello there”。** – Squashman