Solved How to check file for some text?

August 26, 2011 at 04:14:39
Specs: Windows XP
Good Afternoon,

I have a batch file that checks if a file exists, then executes a command (sends an email if condition is true). Now I would like to add an AND command (don't know if it's possible in batch) to check if the file contains a piece of text.

For example, in simple text, something like this:

IF file test.txt exists
AND if test.txt contains "test1"
OR if test.txt contains "test2"
OR if test.txt contains "test3"
THEN execute command

My temporary batch file is as follows:

if exist test.txt
bmail -s (IP address here) -t (e-mail addresses here) -f Test_System -a "Test Problem" -m test.txt
del test.txt

Hence, I would like to add to this batch file the file scanning part stated earlier.

Any help would be appreciated.

Thanks!!


See More: How to check file for some text?

Report •

#1
August 26, 2011 at 15:40:21
✔ Best Answer
Maybe something like this:


1>nul 2>&1 findstr "/c:test1" "/c:test2" "/c:test3" "test.txt" && (
bmail -s (IP address here) -t (e-mail addresses here) -f Test_System -a "Test Problem" -m test.txt
del test.txt
)


Report •

#2
August 27, 2011 at 05:20:44
Hi Judago,

Thanks for your reply!!

Can I ask why there is && before the command that needs to be executed (beginning from bmail...)?

Should I add && between if exist test.txt and 1>nul 2>&1 findstr "/c:test1" "/c:test2" "/c:test3" "test.txt"?

Thanks!


Report •

#3
August 27, 2011 at 06:37:58
You shouldn't add "if exist" at all, findstr is taking care of it.


The "&&" roughly equates to:

if the command prior to "&&" returns an exit code of zero execute then command to the right, otherwise skip execution of the following command.

The actual command is "(" - open a multi-line code block for execution.


Report •

Related Solutions

#4
August 28, 2011 at 06:37:47
Oh I see Judago, now I understand better.

Thanks very much for your help, I will try it out at work and will let you know.


Report •

#5
August 28, 2011 at 08:13:37
I don't know what I could be doing wrong.

I'm trying this command inside the batch file test.bat:

1>nul 2>&1 findstr "/c:test1" "/c:test2" "/c:test3" "test.txt" && (
 echo success
)

I've created the test.txt file. First I left the file blank, then I inserted test1 and saved. However nothing's happening. "success" is not being output for neither examples.

Both batch and text files are in the same directory (desktop).


Report •

#6
August 28, 2011 at 12:34:09
It works for me, so I'm not sure what is happening. I just tested it with this:

>test.txt echo test1
1>nul 2>&1 findstr "/c:test1" "/c:test2" "/c:test3" "test.txt" && (
 echo success
)
pause


Report •

#7
August 28, 2011 at 23:22:02
Hi Judago,

It worked at my office (XP) but not at home (Windows 7), but anyway I only need it at work :-)

May I ask just one last question? Could it be done to match an exact string instead of a substring? So "test1" will be successfull, but "abctest1xyz" or "123test1" or "test1456" will not.

Thanks mate!


Report •

#8
August 29, 2011 at 00:13:04
It depends what you mean by exact, does the whole line need to be only "test1" or does it just need to be a space separated word any where on the line?

If the whole line needs to be only the word, then add the "/x" switch to findstr.

1>nul 2>&1 findstr /x "/c:test1" "/c:test2" "/c:test3" "test.txt" && (
 echo success
)

If it just needs to be a separated word, then adding the "/r" switch and using the regex for beginning and end of word should do the trick:

1>nul 2>&1 findstr /r "/c:\<test1\>" "/c:\<test2\>" "/c:\<test3\>" "test.txt" && (
 echo success
)

As for it not working on win 7, I can't test, maybe I was just trying to be too clever, here is a version more likely to work:

if exist "test.txt" (
    rem Which one ????    

    rem findstr /r "/c:\<test1\>" "/c:\<test2\>" "/c:\<test3\>" "test.txt" 
    rem findstr /x "/c:test1" "/c:test2" "/c:test3" "test.txt"

    if not errorlevel 1 (
        echo success
    )
)


Report •

#9
August 29, 2011 at 00:28:16
That's great, matter solved!

Cheers! :-)


Report •

Ask Question