1

私は、MS Batch ファイルを使い始めたばかりです。入力したディレクトリで 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

4 に答える 4

1

これを置き換えると:

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
于 2013-10-24T14:43:57.040 に答える
0

for ループで %%i の値を finame に割り当てると、前の値が置き換えられるため、最後のファイルのみがコンソールにエコーされます。

(for の外で) findstr コマンドを試すと、ファイルのリストが表示されます。

于 2013-10-24T14:44:27.000 に答える
0

みんな、ありがとう。他の誰かがこのサイトを検索した場合に備えて、これが私の最終的なコードです。

@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
于 2013-10-25T20:38:35.487 に答える