Searching a string within a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fkjhr4e8d
Posts: 8
Joined: 05 Apr 2022 05:02

Searching a string within a text file

#1 Post by fkjhr4e8d » 06 Apr 2022 06:11

I have what should be a simple solution. I have a text file called sendemail.log with the following text in it

Code: Select all

Apr 06 13:28:37 acb-pc sendEmail.exe[2452]: Email was sent successfully!  From: <xxx@yyy.net> To: <zzz@aaa.com> Subject: [This is the subject] Server: [smtp.yyyy.net:99]
I need to search for Email was sent successfully!.

I wrote two scripts and both are not giving the desired results:

Test2.cmd

Code: Select all

REM @echo OFF
setLOCAL EnableDelayedExpansion
set tmp-File=C:\Program Files (x86)\sendEmail\sendemail.log
set email_UpdCnt=0
IF EXIST "%tmp-File%" FOR /F %%A IN ('FIND /C "Email was sent successfully!" < "%tmp-File%"') DO set email_UpdCnt=%%A

echo Arrived here

if %email_UpdCnt% GTR 0 GOTO ExitRT

echo Should not arrived here
exit /b 2

:ExitRT

echo Should arrive here
exit /b 0
The code seems to bomb out after executing the IF EXISTS line.

----

I tried using FINDSTR (with and without CALL before findstr)

Test1.cmd

Code: Select all

REM @echo OFF
setLOCAL EnableDelayedExpansion
set tmp-File=C:\Program Files (x86)\sendEmail\sendemail.log
set email_StrFound=0
findstr /c:"Email was sent successfully!" "%tmp-File%"
set email_StrFound=%errorlevel%
echo Arrived here
if %email_StrFound% EQU 0 GOTO ExitRT

echo Should not arrived here
exit /b 2

:ExitRT

echo Should arrive here
exit /b 0
In this case the execution freezes after executing the line findstr.

Can someone see whether they can replicate my findings and maybe help me understand what is the source of the problem?

Thanks

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Searching a string within a text file

#2 Post by atfon » 06 Apr 2022 09:28

You are just trying to find out if text is present in the file? Why not something as simple as:

Code: Select all

type "%programfiles(x86)%\sendEmail\sendemail.log" |findstr /c:"Email was sent successfully!" >nul 2>&1
if %errorlevel% EQU 0 (
echo do something if found
) else (
echo do something if not found
)
Replace the echo lines with whatever you want to achieve if the text is found or not found.

fkjhr4e8d
Posts: 8
Joined: 05 Apr 2022 05:02

Re: Searching a string within a text file

#3 Post by fkjhr4e8d » 08 Apr 2022 06:46

atfon wrote:
06 Apr 2022 09:28
You are just trying to find out if text is present in the file? Why not something as simple as:

Code: Select all

type "%programfiles(x86)%\sendEmail\sendemail.log" |findstr /c:"Email was sent successfully!" >nul 2>&1
if %errorlevel% EQU 0 (
echo do something if found
) else (
echo do something if not found
)
Replace the echo lines with whatever you want to achieve if the text is found or not found.
I will try it and report back. I need to clarify that the location (and name) of the log file may vary.

I still don't understand what caused the code I posted to hang.

What does the

Code: Select all

2 > &1
actually do?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Searching a string within a text file

#4 Post by ShadowThief » 08 Apr 2022 07:37

fkjhr4e8d wrote:
08 Apr 2022 06:46
I still don't understand what caused the code I posted to hang.

What does the

Code: Select all

2 > &1
actually do?
2 refers to stderr ("standard error"), which is an output stream that things write errors to if they've been coded correctly
> is a redirection operator for moving the contents of output streams somewhere else
& in this particular instance is an indicator that the redirection is going to another output stream
1 refers to stdout ("standard output"), which is the normal output stream where text usually goes

The entire code takes all output going from stderr and redirects it to stdout so that it all gets treated like regular text.

fkjhr4e8d
Posts: 8
Joined: 05 Apr 2022 05:02

Re: Searching a string within a text file

#5 Post by fkjhr4e8d » 08 Apr 2022 13:38

ShadowThief wrote:
08 Apr 2022 07:37
fkjhr4e8d wrote:
08 Apr 2022 06:46
What does the

Code: Select all

2 > &1
actually do?
2 refers to stderr ("standard error"), which is an output stream that things write errors to if they've been coded correctly
> is a redirection operator for moving the contents of output streams somewhere else
& in this particular instance is an indicator that the redirection is going to another output stream
1 refers to stdout ("standard output"), which is the normal output stream where text usually goes

The entire code takes all output going from stderr and redirects it to stdout so that it all gets treated like regular text.
Thanks

fkjhr4e8d
Posts: 8
Joined: 05 Apr 2022 05:02

Re: Searching a string within a text file

#6 Post by fkjhr4e8d » 12 Apr 2022 04:38

@ShadowThief: Thanks. Worked (and I have a better understanding of how to get to the different outputs)

Post Reply