windowsbatch-filefindfindinfiles

Can I search for multiple strings in one "find" command in batch script?


I have a windows batch script that will look for a string within a file

find /i "WD6"  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

Currently this is what my code looks like. I've come across a new string I want to search for in the same file and do the same action if it finds it, it stored it in a variable called %acctg_cyc% can I search for both strings in one line of code? I tried this:

find /i "WD6" %acctg_cyc%  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

But it seems to ignore the %acctg_cyc% and only look for "WD6" in file.txt. I tried testing where %acctg_cyc% is in file.txt and when it is not and it passes both times.

Any thoughts? I know I could do this in more lines of code but I'm really trying to avoid that right now. Maybe it's just not possible.

Thank you for any help!


Solution

  • find isn't very powerful. It searches for one string only (even if it is two words): find "my string" file.txt looks for the string my string.

    findstr has much more power, but you have to be careful how to use it:

    findstr "hello world" file.txt 
    

    finds any line, that contains either hello or world or both of them.

    see findstr /? for more info.

    Finding both words in one line is possible with (find or findstr):

    find "word1" file.txt|find "word2"
    

    finding both words scattered over the file (find or findstr):

    find "word1" file.txt && find "word2" file.txt
    if %errorlevel%==0 echo file contains both words