Batch file findstr/m仅返回找到的第一个文件名
我刚开始对MS批处理文件感到厌烦。我创建了一个小批量,使用findstr/m在输入的目录中搜索包含特定字符串的文件。它返回一个包含字符串的文件,但只返回它找到的第一个字符串。我已经搜索了findstr/?和在线命令参考,以及本网站。我找不到findstr返回包含字符串实例的所有文件的方法。我错过了什么Batch file findstr/m仅返回找到的第一个文件名,batch-file,findstr,Batch File,Findstr,我刚开始对MS批处理文件感到厌烦。我创建了一个小批量,使用findstr/m在输入的目录中搜索包含特定字符串的文件。它返回一个包含字符串的文件,但只返回它找到的第一个字符串。我已经搜索了findstr/?和在线命令参考,以及本网站。我找不到findstr返回包含字符串实例的所有文件的方法。我错过了什么 @echo off setlocal ECHO This Program Searches for words inside files! :Search set /P userin=Ent
@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
如果替换此项:
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=
有了这个,它可能会按照你期望的方式工作
findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found
如果替换此项:
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=
有了这个,它可能会按照你期望的方式工作
findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found
在for循环中,当您将%%i的值指定给finame时,您将替换上一个值,因此只有最后一个文件会回显到控制台
如果您尝试findstr命令(在for之外),您将看到文件列表。在for循环中,当您将%%i的值指定给finame时,您正在替换以前的值,因此只有最后一个文件会回显到控制台 如果您尝试findstr命令(在for之外),您将看到文件列表。要将所有文件放入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
要在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
谢谢大家。以防其他人搜索此网站,这是我的最终代码
@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
谢谢大家。以防其他人搜索此网站,这是我的最终代码
@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
如果有人问我,我会加上停顿,以确保我的ELSE语法是正确的。它可以被删除。如果有人问我,我添加了暂停,这样我就可以确保我的ELSE语法是正确的。它可以被移除。谢谢,我刚刚想到了这一点!我想问是否有一种方法可以将所有的输出放在一个变量中,但我想如果你有很多返回的文件名,这会变得很糟糕。谢谢,我刚刚想出来了!我想问是否有一种方法可以将所有的输出放在一个变量中,但是我想如果你有很多返回的文件名,这会变得很糟糕。