Solved Problem with string comparison in batch script

XJDHDR

New Member
Messages
4
Greetings

I am attempting to write a batch script that uses a program called Mailsend to send an email. The script then examines Mailsend's log output to determine if the email was successfully sent.
This is what I have so far:
Code:
mailsend1.17b14.exe -to <snip personal info> -log temp.txt -attach test.txt
.\Tail\Tail -1 temp.txt > temp2.txt
Del temp.txt
Set /p Message=<temp2.txt

Set Message=%Message:~26%

Set Cmess=Mail sent successfully

Echo %Message%
Echo %Cmess%

If "Message"=="Cmess" ( Echo Correct ) Else ( Echo Wrong )
Here is a rundown of what I am doing:
The first line sends the email.
The second and third use a program called Tail to delete all lines except the last one from the log file (the last line contains the output I'm looking for).
The next 2 lines then assign the contents of the log file to a variable then truncate the first 26 characters (date and time info that I don't need).
The next line creates a second variable that contains the desired output (i.e. "Mail sent successfully")
The next two lines echos the contents of the variables.
And finally, the last line compares the two variable and takes a particular action depending on whether the two variables are equal or not.

The comparison at the end is where I'm having problems. Every time I run the script, the last line I get printed on the screen is "Wrong" (i.e. the "Echo Wrong" command is executed meaning "Message" does not equal "Cmess"). This happens even when the email was sent successfully. I've tried various things and I haven't managed to get the "Echo Correct" command to ever execute, even when the output of the two Echo commands just before is exactly the same. I've even tried changing the "Set Cmess" line to:
Code:
Set /p Cmess=<temp2.txt  
Set Cmess=%Cmess:~26%
...and the output for the comparison is still "wrong". I can only think that there must be something wrong with how I structured the comparison.

Can anyone help me get this script to work or help me script a means of checking the log file for "Mail sent successfully"? I would greatly appreciate any help I get.

If it interests anyone, this is what the log file contains if an email gets sent successfully:
Code:
07-Jun-2014 08:17:28.172: mailsend v@(#) mailsend v1.17b14
07-Jun-2014 08:17:33.011: Mail sent successfully
 

My Computer

System One

  • OS
    Windows 8.1
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Custom built
    CPU
    Intel Core i5 2500K
    Motherboard
    MSI Z77A-G43
    Memory
    16GB DDR3 133MHz
    Graphics Card(s)
    Geforce 580
    Browser
    Palemoon (Optimised Firefox)
    Antivirus
    Windows Defender & Emsisoft Antimalware
Try this:

Set Cmess="Mail sent successfully"
 

My Computer

System One

  • OS
    Windows 10 Pro X64
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Lenovo IdeaCenter K450
    CPU
    Intel Quad Core i7-4770 @ 3.4Ghz
    Motherboard
    Lenovo
    Memory
    16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
    Graphics Card(s)
    Intel Integrated HD Graphics
    Sound Card
    Realtek HD Audio
    Monitor(s) Displays
    HP h2207
    Screen Resolution
    1680x1050@59Hz
    Hard Drives
    250GB Samsung EVO SATA-3 SSD;
    2TB Seagate ST2000DM001 SATA-2;
    1.5TB Seagate ST3150041AS SATA
    PSU
    500W
    Keyboard
    Wired USB
    Mouse
    Wired USB
    Internet Speed
    3GB Up, 30GB Down
    Browser
    SeaMonkey
    Antivirus
    Windows Defender; MBAM Pro
    Other Info
    UEFI/GPT
    PLDS DVD-RW DH16AERSH
Thank you for the reply Ztrucker.

Unfortunately, your suggestion didn't work. That command causes Cmess to be defined with the quotation marks as well.

Fortunately, I have managed to find out what I did wrong: I didn't define the two variables in the If comparison as variables by surrounding them with percentages. The fixed script is:
Code:
mailsend1.17b14.exe -to <snip personal info> -log temp.txt -attach test.txt
.\Tail\Tail -1 temp.txt > temp2.txt
Del temp.txt
Set /p Message=<temp2.txt

Set Message=%Message:~26%

Set Cmess=Mail sent successfully

Echo %Message%
Echo %Cmess%

If "%Message%"=="%Cmess% " ( Echo Correct ) Else ( Echo Wrong )

I also managed to find a shorter way of writing this script by using the Findstr command:
Code:
mailsend1.17b14.exe -to <snip personal info> -log temp.txt -attach test.txt
Findstr /I /M /C:"Mail sent successfully" Temp.txt >nul
If Not %ErrorLevel% == 0 ( Echo Correct ) Else ( Echo Wrong )

This new script also correctly detects when the log file contains "Mail sent successfully".

Edit: Finally found the problem in the original script.
 

My Computer

System One

  • OS
    Windows 8.1
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Custom built
    CPU
    Intel Core i5 2500K
    Motherboard
    MSI Z77A-G43
    Memory
    16GB DDR3 133MHz
    Graphics Card(s)
    Geforce 580
    Browser
    Palemoon (Optimised Firefox)
    Antivirus
    Windows Defender & Emsisoft Antimalware
Yes that's the correct way of doing it - your initial script was only reading line 1 after char 26 to EOL.(i.e: "mailsend v@(#) mailsend v1.17b14") - you would have had to loop to EOF otherwise.
 

My Computer

System One

  • OS
    PC-DOS v1.0
    Computer type
    PC/Desktop
    System Manufacturer/Model
    IBM
    CPU
    Intel 8088, 4.77MHz
    Memory
    16K, 640K max
    Graphics Card(s)
    What's that?
    Sound Card
    Not quite
    Screen Resolution
    80 X 24 text
    Hard Drives
    dual 160KB 5.25-inch disk drives
Yes that's the correct way of doing it - your initial script was only reading line 1 after char 26 to EOL.(i.e: "mailsend v@(#) mailsend v1.17b14") - you would have had to loop to EOF otherwise.
That would have been the case if I didn't manipulate the log file before reading it into the Message variable.
You will notice that between sending the email that created the log file and reading the contents into a variable, I executed a program called Tail (.\Tail\Tail). In this case, it deletes all but the last line of the log file (-1 temp.txt), leaving me with a single line that says: "07-Jun-2014 08:17:33.011: Mail sent successfully".
Character 26 to EOL thus becomes: "Mail sent successfully".
 

My Computer

System One

  • OS
    Windows 8.1
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Custom built
    CPU
    Intel Core i5 2500K
    Motherboard
    MSI Z77A-G43
    Memory
    16GB DDR3 133MHz
    Graphics Card(s)
    Geforce 580
    Browser
    Palemoon (Optimised Firefox)
    Antivirus
    Windows Defender & Emsisoft Antimalware
Oh I see - do you also get a trailing space from the text file (with both lines retained - change var2 to var1 for truncated file)? This is a modified script - I prefer findstr though

Code:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set x=0
for /F "tokens=*" %%A in (temp2.txt) do (
    SET /A x=!x! + 1
    set var!x!=%%A
)

Set Message=%var2:~26% 
Set Cmess=Mail sent successfully

Echo %Message%
Echo %Cmess%

If "%Message:~0,-1%" == "%Cmess%" (Echo Correct )

Edit: Just realised it's actually the EOL char that needs to be truncated (not trailing space)
 

My Computer

System One

  • OS
    PC-DOS v1.0
    Computer type
    PC/Desktop
    System Manufacturer/Model
    IBM
    CPU
    Intel 8088, 4.77MHz
    Memory
    16K, 640K max
    Graphics Card(s)
    What's that?
    Sound Card
    Not quite
    Screen Resolution
    80 X 24 text
    Hard Drives
    dual 160KB 5.25-inch disk drives

My Computer

System One

  • OS
    Windows 8.1
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Custom built
    CPU
    Intel Core i5 2500K
    Motherboard
    MSI Z77A-G43
    Memory
    16GB DDR3 133MHz
    Graphics Card(s)
    Geforce 580
    Browser
    Palemoon (Optimised Firefox)
    Antivirus
    Windows Defender & Emsisoft Antimalware
Back
Top