Findstr不喜欢变量输入中的空格。

时间:2017-02-03 20:47:10

标签: batch-file command-line whitespace findstr

如果我不在srch字符串中的查询之间使用空格,则下面的批处理文件非常有用。一旦我添加一个空格,程序跳出并退出。

@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

1 个答案:

答案 0 :(得分:1)

您可以将findstr行更改为:

findstr/IC:"%srch%" "%USERPROFILE%\documents\names.txt"

替代使用Find

find /I "%srch%"<"%USERPROFILE%\documents\names.txt"