Findstr and multi-string AND searches

Discussion in 'Microsoft Windows 2000 CMD Promt' started by Michel Gallant, Oct 12, 2003.

  1. 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
     
    Michel Gallant, Oct 12, 2003
    #1
    1. Advertisements

  2. Michel Gallant

    Marty List Guest

    "Michel Gallant" <> wrote in message
    news:...
    > 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
    )
     
    Marty List, Oct 12, 2003
    #2
    1. Advertisements

  3. 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

    "Marty List" <> wrote in message
    news:bmc16k$k1hrk$-berlin.de...
    >
    > "Michel Gallant" <> wrote in message
    > news:...
    > > 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
    > )
    >
    >
     
    Michel Gallant, Oct 13, 2003
    #3
  4. "Michel Gallant" schrieb:

    >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
     
    Matthias Tacke, Oct 13, 2003
    #4
  5. 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

    "Matthias Tacke" <> wrote in message news:bme57p$efj$05$-online.com...
    > "Michel Gallant" schrieb:
    >
    > >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
     
    Michel Gallant, Oct 13, 2003
    #5
  6. "Michel Gallant" schrieb:

    >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


    --
    Phil Robyn
    Univ. of California, Berkeley
     
    Matthias Tacke, Oct 13, 2003
    #6
  7. Michel Gallant

    guard Guest

    "Michel Gallant" wrote
    > 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!
     
    guard, Oct 13, 2003
    #7
  8. Thanks for these references.
    - Mitch
    http://pages.istar.ca/~neutron

    "guard" <^T^S^G^n^e^w^s^@TheSystemGuard.com> wrote in message
    news:bmeffl$ler0r$-berlin.de...
    > "Michel Gallant" wrote
    > > 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!
    >
    >
     
    Michel Gallant, Oct 13, 2003
    #8
  9. 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

    "Matthias Tacke" <> wrote in message news:bme57p$efj$05$-online.com...
    > "Michel Gallant" schrieb:
    >
    > >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
     
    Michel Gallant, Oct 13, 2003
    #9
  10. "Michel Gallant" schrieb:

    >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
     
    Matthias Tacke, Oct 13, 2003
    #10
  11. 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

    "Matthias Tacke" <> wrote in message news:bmfakl$op2$05$-online.com...
    > "Michel Gallant" schrieb:
    >
    > >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
     
    Michel Gallant, Oct 14, 2003
    #11
  12. Michel Gallant

    Ritchie Guest

    "Michel Gallant" <> wrote in message news:...
    > 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.

    --
    Ritchie, undo for mail
     
    Ritchie, Oct 14, 2003
    #12
  13. "Ritchie" <> wrote in message
    news:bmg2qb$m55hs$-berlin.de...
    > "Michel Gallant" <> wrote in message

    news:...
    > > 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.
    >

    Thanks Ritchie. That works properly now.
    Cheers,
    - Mitch
     
    Michel Gallant, Oct 14, 2003
    #13
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MordacNM

    Re: Findstr problem within batch

    MordacNM, Jul 29, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    5
    Views:
    9,052
    M Ward
    Jul 29, 2003
  2. Jon Smith

    Findstr question

    Jon Smith, Mar 25, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    8
    Views:
    472
    Paul R. Sadowski
    Mar 25, 2004
  3. djc

    findstr, for, or other command to do this?

    djc, Apr 27, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    8
    Views:
    1,311
    Matthias Tacke
    Apr 28, 2004
  4. Alex Horan

    Extracting a sub-string from a FINDSTR command

    Alex Horan, Nov 1, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    4
    Views:
    6,110
    Matthias Tacke
    Nov 1, 2004
  5. erony

    Findstr and Bmail

    erony, Feb 15, 2012, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    1,791
    erony
    Feb 20, 2012
Loading...

Share This Page