Page 1 of 1

Searching a string within a text file

Posted: 06 Apr 2022 06:11
by fkjhr4e8d
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

Re: Searching a string within a text file

Posted: 06 Apr 2022 09:28
by atfon
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.

Re: Searching a string within a text file

Posted: 08 Apr 2022 06:46
by fkjhr4e8d
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?

Re: Searching a string within a text file

Posted: 08 Apr 2022 07:37
by ShadowThief
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.

Re: Searching a string within a text file

Posted: 08 Apr 2022 13:38
by fkjhr4e8d
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

Re: Searching a string within a text file

Posted: 12 Apr 2022 04:38
by fkjhr4e8d
@ShadowThief: Thanks. Worked (and I have a better understanding of how to get to the different outputs)