Search for files that contain both words...
Moderator: DosItHelp
Search for files that contain both words...
What is the simplest possible way to search for files in the current directory that contain both the words "DOS" and "batch"?
Thanks in advance!
Thanks in advance!
Re: Search for files that contain both words...
Code: Select all
findstr "DOS" *.txt > withDOS.tmp
for /F "delims=:" %%a in ('findstr "batch" *.txt') do (
findstr /C:"%%a" withDOS.tmp > NUL && echo %%a
)
del withDOS.tmp
Re: Search for files that contain both words...
One liner to run on the command line:
Change %F to %%F if used in a batch file
Dave Benham
Code: Select all
for /f "eol=: delims=" %F in ('findstr /m "DOS" *') do @findstr /m "batch" "%F"
Change %F to %%F if used in a batch file
Dave Benham
Re: Search for files that contain both words...
I am not getting any results except for the batch file itself. I even added an /i to the findstr commands. I probably should have said that these two words aren't surrounded by spaces.
The batch file can't find file.txt below:
file.txt
----------------------------------------------------
Diff_DOS.UB</set_f><agent>:WIN32\</agent>
<files_written>4862</files_written>
_file_batch>Diff_FBF.UB</set_file_name>
-----------------------------------------------
PS: I much prefer a one batch file that doesnt create / delete temp files.
The batch file can't find file.txt below:
file.txt
----------------------------------------------------
Diff_DOS.UB</set_f><agent>:WIN32\</agent>
<files_written>4862</files_written>
_file_batch>Diff_FBF.UB</set_file_name>
-----------------------------------------------
PS: I much prefer a one batch file that doesnt create / delete temp files.
Re: Search for files that contain both words...
How can I do this using the find command? Findstr wont work with unicode files.
I tried to convert the above example using the find command instead of findstr, but it's not working right.
for /f "eol=: delims=" %%F in ('find /c "DOS" *') do @find /c "batch" "%%F"
I tried to convert the above example using the find command instead of findstr, but it's not working right.
for /f "eol=: delims=" %%F in ('find /c "DOS" *') do @find /c "batch" "%%F"
Re: Search for files that contain both words...
Code: Select all
@echo off
for /f "delims=:" %%A in ('2^>nul find /c "DOS" * ^| findstr /levc:": 0"') do (
for /f "tokens=1*" %%B in ("%%A") do >nul find "batch" "%%C" && echo %%~nxC
)
Dave Benham
Re: Search for files that contain both words...
Hi Dave, thanks so much for taking the time to help. Unfortunately, that code doesnt produce any results; not even the non-unicode text files. The last one-line code was able to produce results; however, only for non-unicode files.
I definitely get hits if I manually type find /c "DOS * from the command line.
I definitely get hits if I manually type find /c "DOS * from the command line.
Re: Search for files that contain both words...
Hmmm
I tested the code on Vista, and it worked find with both ANSI and unicode files.
It may take a while to do the initial FIND scan. Have you tried it on a small directory?
Dave Benham


I tested the code on Vista, and it worked find with both ANSI and unicode files.
It may take a while to do the initial FIND scan. Have you tried it on a small directory?
Dave Benham
Re: Search for files that contain both words...
This works for me:
Of course it will find your batch procedure.
Saso
Code: Select all
@echo off
for /f "tokens=1* delims=" %%A in ('dir/on/b *.*') do (
find /I "DOS" %%A>nul 2>nul&&find /I "batch" %%A>nul 2>nul&&echo %%A
)
goto :EOF
Of course it will find your batch procedure.
Saso
Re: Search for files that contain both words...
I recreated the script; now, it works perfectly. I am pretty sure whatever the problem was, it was user error. I was even able to replace the the words DOS and batch with the variables I ultimately needed to use.
Thanks a million; and, sorry to doubt you
Thanks a million; and, sorry to doubt you

Re: Search for files that contain both words...
I feel reall bad for asking this, but what if I wanted to further filter out the results but NOT displaying files that have the string _OS_
EDIT: Below, is my second attempt and further filtering out the results by not displaying any files that contain _OS_ However, it's not correctly obviously.
EDIT: Below, is my second attempt and further filtering out the results by not displaying any files that contain _OS_ However, it's not correctly obviously.
Code: Select all
@echo off
for /f "tokens=1* delims=" %%A in ('dir/on/b *.*') do (
find /I "DOS" %%A>nul 2>nul&&find /I "disk" %%A>nul 2>nul&&find /I "_OS_" %%A>nul 2>nul&&if not errorlevel 1 goto :EOF&&echo %%A
)
goto :EOF
Re: Search for files that contain both words...
@miskox - I tried something similar, but it is much slower than the solution I posted. I actually used a simpler and more efficient version that used a simple FOR and eliminated the DIR. But it was still much slower than the solution I posted.
The problem is FIND (or FINDSTR) must be called at least once for every file in the directory. There is initialization overhead that must take place for each instantiation. My solution calls FIND only once and FINDSTR only once for the entire directory for the first filter, so it eliminates a lot of overhead.
@MKANET - The /V option will look for the existence of at least one line that does not contain the string. That is not what you want. You want to throw away files that contain at least one line that contains the string. So you need to look for the string and treat an error condition (not found) as success.
Here is my solution extended to exclude files containing "_OS_"
Dave Benham
The problem is FIND (or FINDSTR) must be called at least once for every file in the directory. There is initialization overhead that must take place for each instantiation. My solution calls FIND only once and FINDSTR only once for the entire directory for the first filter, so it eliminates a lot of overhead.
@MKANET - The /V option will look for the existence of at least one line that does not contain the string. That is not what you want. You want to throw away files that contain at least one line that contains the string. So you need to look for the string and treat an error condition (not found) as success.
Here is my solution extended to exclude files containing "_OS_"
Code: Select all
@echo off
for /f "delims=:" %%A in ('2^>nul find /c "DOS" * ^| findstr /levc:": 0"') do (
for /f "tokens=1*" %%B in ("%%A") do >nul find "batch" "%%C" && (
>nul find "_OS_" "%%C" || echo %%~nxC
)
)
Dave Benham
Re: Search for files that contain both words...
Thanks Dave, it works perfectly; even the excluding of _OS_