findstr /m只返回第一个找到的文件名

时间:2022-07-17 13:53:28

I am just starting to get my feet wet with MS Batch files. I have created a small batch that searches the entered directory for files containing a certain string using findstr /m. It is returning a file that contains the string, but only the first one it finds. I have searched the findstr /? and online command reference, as well as this site. I cannot find a way for findstr to return ALL the files with an instance of the string. What am I missing?

我刚刚开始接触MS批处理文件。我创建了一个小批,使用findstr /m搜索输入目录中包含特定字符串的文件。它返回一个包含字符串的文件,但只返回它找到的第一个字符串。我已经搜索了findstr /?以及在线命令引用,以及这个站点。我找不到findstr返回所有带有字符串实例的文件的方法。我缺少什么?

@echo off
setlocal 
ECHO This Program Searches for words inside files!  
:Search
set /P userin=Enter Search Term: 
set /p userpath=Enter File Path: 
FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i  
if "%finame%" == "" (set finame=No matching files found)
echo %finame% 
set finame=
endlocal
:searchagain
set /p userin=Do you want to search for another file? (Y/N): 
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
Pause
:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain

4 个解决方案

#1


1  

If you replace this:

如果你更换:

FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i  
if "%finame%" == "" (set finame=No matching files found)
echo %finame% 
set finame=

with this then it may work the way you expect

有了这个,它可能会以你所期望的方式工作

findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found

#2


0  

In your for loop, when you assign to finame the value of %%i, you are replacing the previous value, so only the last file gets echoed to console.

在for循环中,当您将%%i的值赋给finame时,您将替换先前的值,因此只有最后一个文件被回显到控制台。

If you try your findstr command (out of the for) you will see the list of files.

如果您尝试了findstr命令(out of for),您将看到文件列表。

#3


0  

to place all in var:

在var中放置所有:

setlocal enabledelayedexpansion
set nlm=^


set nl=^^^%nlm%%nlm%^%nlm%%nlm%
for %%i in ('dir %userpath% /b') do for /f %%a in ('type "%userpath%\%%i"^|find "%userin%" /i') do set out=!out!!nl!%%i

#4


0  

Thanks everyone. Just in case someone else searches this site, here is my final code.

谢谢每一个人。如果有人搜索这个网站,这是我的最终代码。

@echo off

ECHO This Program Searches for words inside files!  

:Search
setlocal
set /P userin=Enter Search Term: 
set /p userpath=Enter File Path: 
findstr /M /S /I /P /C:%userin% %userpath%\*.*  2> NUL
if ERRORLEVEL 1 (ECHO No Matching Files found) ELSE (
GOTO searchagain
)
endlocal

:searchagain
setlocal
set /p userin=Do you want to search for another file? (Y/N): 
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
endlocal

:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain

#1


1  

If you replace this:

如果你更换:

FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i  
if "%finame%" == "" (set finame=No matching files found)
echo %finame% 
set finame=

with this then it may work the way you expect

有了这个,它可能会以你所期望的方式工作

findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found

#2


0  

In your for loop, when you assign to finame the value of %%i, you are replacing the previous value, so only the last file gets echoed to console.

在for循环中,当您将%%i的值赋给finame时,您将替换先前的值,因此只有最后一个文件被回显到控制台。

If you try your findstr command (out of the for) you will see the list of files.

如果您尝试了findstr命令(out of for),您将看到文件列表。

#3


0  

to place all in var:

在var中放置所有:

setlocal enabledelayedexpansion
set nlm=^


set nl=^^^%nlm%%nlm%^%nlm%%nlm%
for %%i in ('dir %userpath% /b') do for /f %%a in ('type "%userpath%\%%i"^|find "%userin%" /i') do set out=!out!!nl!%%i

#4


0  

Thanks everyone. Just in case someone else searches this site, here is my final code.

谢谢每一个人。如果有人搜索这个网站,这是我的最终代码。

@echo off

ECHO This Program Searches for words inside files!  

:Search
setlocal
set /P userin=Enter Search Term: 
set /p userpath=Enter File Path: 
findstr /M /S /I /P /C:%userin% %userpath%\*.*  2> NUL
if ERRORLEVEL 1 (ECHO No Matching Files found) ELSE (
GOTO searchagain
)
endlocal

:searchagain
setlocal
set /p userin=Do you want to search for another file? (Y/N): 
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
endlocal

:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain