Search for files that contain both words...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Search for files that contain both words...

#1 Post by MKANET » 27 Apr 2012 19:48

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!

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Search for files that contain both words...

#2 Post by Aacini » 27 Apr 2012 20:08

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search for files that contain both words...

#3 Post by dbenham » 27 Apr 2012 20:40

One liner to run on the command line:

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

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#4 Post by MKANET » 27 Apr 2012 21:05

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.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#5 Post by MKANET » 27 Apr 2012 21:12

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"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search for files that contain both words...

#6 Post by dbenham » 28 Apr 2012 00:54

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

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#7 Post by MKANET » 28 Apr 2012 01:16

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search for files that contain both words...

#8 Post by dbenham » 28 Apr 2012 01:37

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

miskox
Posts: 666
Joined: 28 Jun 2010 03:46

Re: Search for files that contain both words...

#9 Post by miskox » 28 Apr 2012 11:01

This works for me:

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

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#10 Post by MKANET » 28 Apr 2012 11:29

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 :)

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#11 Post by MKANET » 28 Apr 2012 12:17

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.

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


dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Search for files that contain both words...

#12 Post by dbenham » 28 Apr 2012 12:46

@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_"

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

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Search for files that contain both words...

#13 Post by MKANET » 28 Apr 2012 17:53

Thanks Dave, it works perfectly; even the excluding of _OS_

Post Reply