2017-02-03 58 views
0

如果我不在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 
+0

'FINDSTR /我 “%SRCH%” %USERPROFILE %\ documents \ names.txt' – Squashman

+1

帮助文件显示了一些很好的例子:**使用空格分隔多个搜索字符串,除非argume nt前缀为/ C。例如,'FINDSTR'hello there“x.y”在文件x.y中搜索“hello”或“there”。 'FINDSTR/C:'hello there“x.y”在文件x.y中搜索“hello there”。** – Squashman

回答

1

您可以简单地改变你的findstr一行改为:

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

使用Find备选:

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

在开始的帖子中更新您的脚本,使用我的命令显示它并再次解释问题。正如你已经确定我的命令在控制台中工作,显然你的脚本有问题。 – Compo

+0

写入的脚本在命令提示符下工作。但是,当放置在批处理文件中时,它会跳出命令行。你的方式回来了一个坏的开关“c:” –

+0

删除**'/ L' **,我已经在代码中这样做了,另外你有没有想过放弃**'findstr' **,并简单地使用* *'find/i“%srch%”** – Compo