具有多个搜索结果的'findstr'(批处理)



这是基于我上一个问题的脚本:,目录映射不起作用(批处理)

(对于整个脚本,请单击链接。但是,只有当"目录映射输出不起作用"中的以下代码截图对您来说没有意义时,您才能单击该链接)

<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"命令从一个搜索词输出许多搜索结果。代码格式不正确吗?请就此问题发送消息/评论。

如果您只想显示匹配的行,则完全放弃 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!

最新更新