'findstr'有多个搜索结果(BATCH)

时间:2013-09-05 12:12:34

标签: batch-file

这是基于我上一个问题的脚本:Directory mapout is not working (BATCH)

(对于整个脚本,请单击链接。但是,如果“目录mapout中的以下代码片段无效”对您没有任何意义,则您只能单击该链接)

<code>
cd temporary
set odir=%dir%
set /p cdir="DIRECTORY: " 
set domap=%cdir%
title SONOROUS FILE SEARCHER: Mapping out...
echo PLEASE WAIT, MAPPING OUT DIRECTORY.
dir %domap% /a-d /b /s > "tempres.rsm"
echo Directory Mapout done
echo -----------------------------
echo       DIRECTORY MAPOUT
set dirmapout=<tempres.rsm
echo %dirmapout%
echo -----------------------------
title SONOROUS FILE SEARCHER: Mapout done.
set /p "searchinput=Search Term: "
title SONOROUS FILE SEARCHER (Copyright 2013 by Sonorous)
for /f "delims=" %%a in ('findstr /i /L /c:"%searchinput%" "tempres.rsm" ') do set "found=%%a"
set proin=%found%
echo "%found%"
cd temporary
del "tempres.rsm"
</code>

我希望“for / f”命令从一个搜索词输出许多搜索结果。 代码格式不正确吗?请留言/评论这个问题。

1 个答案:

答案 0 :(得分:1)

如果您只想显示匹配的行,则完全抛弃FOR / F

title SONOROUS FILE SEARCHER (Copyright 2013 by Sonorous)
findstr /i /L /c:"%searchinput%" "tempres.rsm"
cd temporary
del "tempres.rsm"

如果你需要一个匹配线的“数组”,那么:

:: Define the array of matching lines
set "foundCount=0"
for /f delims^=^ eol^= %%a in ('findstr /i /L /c:"%searchinput%" "tempres.rsm" ') do (
  set /a "foundCount+=1"
  setlocal enableDelayedExpansion
  for %%N in (!foundCount!) do (
    endlocal
    set "found%%N=%%a"
  )
)

:: Display the array values
setlocal enableDelayedExpansion
for /l %%N in (1 1 %foundCount%) do echo match %%N = !found%%N!