在文件夹/子文件夹中所有文件内的文本文件中搜索字符串的批处理文件 [英] batch file that searches for strings in a text file inside all files in folders/subfolders
问题描述
我有一个文本文件(sample.txt),其中包含近500个字符串,我有一个文件夹(文件包),其中包含许多子文件夹,这些子文件夹中包含许多文件类型不同的文件(.xml,.cpp,.hpp等) ).
I have a text file (sample.txt) which has almost 500 strings and I have folder (package) which has many sub-folders with many files of different file types(.xml, .cpp, .hpp, few more).
Sample.txt看起来像
Sample.txt looks like
gi_70_1
IF_MNE_70F_BLACK
Backserver
Type_Gradient
Area_round
我需要在"package"文件夹中的所有文件中搜索这些字符串,并打印在Result.txt中找到它的路径
I need to search these strings in all the files in the "package" folder and print the path where it is found in Result.txt
这是我到目前为止所管理的
Here is what I had managed till now
@echo off
set RESULT_FILE="result.txt"
for /F %%i in (sample.txt) do (
pushd %~p0
type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /B /S *.txt') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c "%%i" "%%a"') do (
for /f "tokens=*" %%f in ('find /i "%%i" "%%a"') do if %%c neq 0 echo %%f
)
) >> "%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %RESULT_FILE% >nul 2>&1
)
:: Open the file
"%RESULT_FILE%"
popd
但这只会打印找到的搜索字符串名称,也不是全部搜索出来的字符串.
But this prints only the search string name that is found and that too last searched string, not all.
有人可以帮我吗
推荐答案
findstr /m /L /i /g:sample.txt "Package\*"
应在包目录中找到所有包含sample.txt
文件中任何字符串的文件.
should find all files in your package directory that contain any of the strings in your sample.txt
file.
有关提示,请参见提示中的findstr /?
.当然,您需要用包"的路径名代替
See findstr /?
from the prompt for documentation. Naturally, you'll need to substrtute you pathname for "package"
也将/s
开关添加到搜索子目录中.
add the /s
switch to search subdirectories too.
在命令中添加>filename
以将输出放入文件中.
Add >filename
to command to put the output into a file.
添加if errorlevel 1 echo not found>filename
,如果找不到字符串,则将字符串not found
放入文件中
Add if errorlevel 1 echo not found>filename
to put the string not found
into the file if no strings are found
这篇关于在文件夹/子文件夹中所有文件内的文本文件中搜索字符串的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!