对于批处理多行中的/ f命令

时间:2015-02-20 01:20:07

标签: batch-file

我正在尝试搜索文本文件,但我遇到了问题:

Test.txt的:

Pizza|Cheese
Filet Mignon|Meat
Bread|Dough

如何使用“for / f”命令查看所有行,然后回显一件事。 这就是我现在所处的位置:

for /f "tokens=2*delims=|" %a in (test.txt) do echo %a

这回声奶酪,肉和面团。如何隔离线条,以便/ f在这些线条中查看,而不是其他线条。

这就是我想要做的事情:

@echo off
set /p Quest="What do you want to eat? "
for /f "tokens=2*delims=|" %a in (test.txt) do (
if Quest=Pizza echo ???         <===This is the area where I am having problems
if Quest="Filet mignon" echo ??? <=What are the commands so I can echo only Cheesy, Meaty, or Dough?
if Quest=Bread echo ???        <===Because what is happening right now is that everything is displayed.
)
Pause

2 个答案:

答案 0 :(得分:0)

您忘记了变量所需的%。我想这就是你想要的:

@echo off
set /p Quest="What do you want to eat? "
for /f "tokens=1* delims=^|" %%a in (test.txt) do if "%Quest%"=="%%~a" Echo %%b
pause

要求用户选择食品,然后回复该食品的成分。

答案 1 :(得分:0)

我认为你打算:

@echo off
set /p Quest="What do you want to eat? "
for /f "tokens=1,2 delims=|" %%a in (test.txt) do (
  if /i "%Quest%"=="%%a" echo %%b
)
Pause

如果test.txt很大,使用FINDSTR查找所需的行会更有效:

@echo off
set /p Quest="What do you want to eat? "
for /f "tokens=2 delims=|" %%a in ('findstr /ibc:"%Quest%|" test.txt') do echo %%a
Pause