Findstr and multi-string AND searches

M

Michel Gallant

Can findstr.exe be used for a multi-string AND search?
Is that possible given the limited RegExp capability of Findstr?

I want to search multiple files, subdirectory recursive, and
only find files containing BOTH (or more than 2) strings.

The following command finds 2 search words (in the order
given), but only if they are on the same line:
findstr /s /i /m "Word1.*Word2" *.*

Thanks,
- Michel Gallant
Visual Security MVP
 
M

Marty List

Michel Gallant said:
Can findstr.exe be used for a multi-string AND search?
Is that possible given the limited RegExp capability of Findstr?

I want to search multiple files, subdirectory recursive, and
only find files containing BOTH (or more than 2) strings.

The following command finds 2 search words (in the order
given), but only if they are on the same line:
findstr /s /i /m "Word1.*Word2" *.*

Thanks,
- Michel Gallant
Visual Security MVP

This isn't the most efficient solution since it will read each file twice,
but you could use the "&&" conditional processing symbol to execute a second
findstr only if the first one succeeds. Something like this:

@Echo Off
SetLocal
Set SOURCE=C:\Temp
For /R %SOURCE% %%A In (*.*) Do (
findstr /i "Word1" %%A>NUL && findstr /i /m "Word2" %%A
)
 
M

Michel Gallant

Thanks Marty. That seems to do the trick (refreshing my bat knowledge)

Is there a way to filter out (to Nul) lines in the resursion that fail like:
FINDSTR: Cannot open C:\Progra .....

Thanks,
- Mitch
 
M

Matthias Tacke

Michel Gallant said:
Thanks Marty. That seems to do the trick (refreshing my bat knowledge)

Is there a way to filter out (to Nul) lines in the resursion that fail like:
FINDSTR: Cannot open C:\Progra .....
Have you tried to redirect the error output with 2>NUL?

More on this you will find with Start/Help/Search for redirection.

hth
Matthias
 
M

Michel Gallant

Thanks.
Is there a recommended recent Batch file programming book
which includes extra features available for W2k+ ??
I have somewhere "MS-DOS Batch Files" K., Jamsa 1993

- Mitch
 
M

Matthias Tacke

Michel Gallant said:
Thanks.
Is there a recommended recent Batch file programming book
which includes extra features available for W2k+ ??
I have somewhere "MS-DOS Batch Files" K., Jamsa 1993

- Mitch
Btw. you can start with help on the commandline and then
help command. Or search the windows help for the topics you are
interested in. The websites are mostly a compilation of these sources.

hth
Matthias

*** I copied the following out of Message in a.m.b.nt.***
Tim Hill, _Windows NT Shell Scripting_

<http://www.seanet.com/~shardy/ntscript.html>
Win NT CMD programming

<http://www.ss64.com/nt>
Simon Sheppard's WinNT Command Reference
 
G

guard

Is there a recommended recent Batch file programming book
which includes extra features available for W2k+ ??
I have somewhere "MS-DOS Batch Files" K., Jamsa 1993

- Mitch

The FindStr Page will show you the difference between FindStr for
NT/2K/XP/K3
(http://TheSystemGuard.com/TheGuardBook/CCS-Ext/FindStr.htm)

For comparisons of all internal Cmd.exe commands, see
(http://TheSystemGuard.com/TheGuardBook/CCS-Int)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!
 
M

Michel Gallant

OK, here is what I have for simple case there bat
reads in 2 args as search strings.

@Echo Off
SetLocal
Set SOURCE=C:\Progra~1\.......
For /R %SOURCE% %%A In (*.*) Do (findstr %1 %%A>NUL 2>Nul && findstr /m %2 %%A)

How do I modify this if the Set SOURCE= contains a string with explicit spaces (e.g. Program Files
....)
instead of 8.3 path names?

Thanks,
- Mitch
 
M

Matthias Tacke

Michel Gallant said:
OK, here is what I have for simple case there bat
reads in 2 args as search strings.

@Echo Off
SetLocal
Set SOURCE=C:\Progra~1\.......

Set Source="C:\Program Files\...."
Put _either_ Set or the "%SOURCE%" in Parenthes's
For /R %SOURCE% %%A In (*.*) Do (findstr %1 %%A>NUL 2>Nul && findstr >/m %2 %%A)
How do I modify this if the Set SOURCE= contains a string with
explicit spaces (e.g. Program Files ...)
instead of 8.3 path names?
hth
Matthias
 
M

Michel Gallant

That doesn't work.
I tried both approaches individually, or combined and
always get an error (after removing 2>Nul)
FINDSTR: Cannot open C:\Program

- Mitch
 
R

Ritchie

Michel Gallant said:
OK, here is what I have for simple case there bat
reads in 2 args as search strings.

@Echo Off
SetLocal
Set SOURCE=C:\Progra~1\.......
For /R %SOURCE% %%A In (*.*) Do (findstr %1 %%A>NUL 2>Nul && findstr /m %2 %%A)

How do I modify this if the Set SOURCE= contains a string with explicit spaces
(e.g. Program Files

@echo off & setlocal ENABLEEXTENSIONS
set dir="c:\program files"
for /r %dir% %%a in (*.*) do (
find %1 "%%a" >nul && find %2 "%%a" >nul && echo %%a
)

Note that the FIND search string must be quoted. Either hardcode the
quotes (if your search strings never contain spaces) of use quotes on
the commandline.
 
M

Michel Gallant

Ritchie said:
@echo off & setlocal ENABLEEXTENSIONS
set dir="c:\program files"
for /r %dir% %%a in (*.*) do (
find %1 "%%a" >nul && find %2 "%%a" >nul && echo %%a
)

Note that the FIND search string must be quoted. Either hardcode the
quotes (if your search strings never contain spaces) of use quotes on
the commandline.
Thanks Ritchie. That works properly now.
Cheers,
- Mitch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top