Searching a string within a text file
Posted: 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
I need to search for Email was sent successfully!.
I wrote two scripts and both are not giving the desired results:
Test2.cmd
The code seems to bomb out after executing the IF EXISTS line.
----
I tried using FINDSTR (with and without CALL before findstr)
Test1.cmd
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
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 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
----
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
Can someone see whether they can replicate my findings and maybe help me understand what is the source of the problem?
Thanks