Windows 8 and 8.1 Forums


Problem with string comparison in batch script

  1. #1


    Posts : 4
    Windows 8.1

    Problem with string comparison in batch script


    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 System SpecsSystem Spec

  2. #2


    Try this:

    Set Cmess="Mail sent successfully"
      My System SpecsSystem Spec

  3. #3


    Posts : 4
    Windows 8.1


    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 System SpecsSystem Spec

  4. #4


    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 System SpecsSystem Spec

  5. #5


    Posts : 4
    Windows 8.1


    Quote Originally Posted by Superfly View Post
    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 System SpecsSystem Spec

  6. #6


    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 System SpecsSystem Spec

  7. #7


    Posts : 4
    Windows 8.1


    Quote Originally Posted by Superfly View Post
    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)?
    Funny enough, I do. That's why I had to put a space after %Cmess%.

    Quote Originally Posted by Superfly View Post
    I prefer findstr though
    No disagreement here.
      My System SpecsSystem Spec

Problem with string comparison in batch script
Related Threads
Hey guys. just finished formating my computer (using the built in windows 8 feature). once it was up and running i uninstalled all the built in stuff lenovo brings with the computer using revo uninstaller. in the procces i also removed all registry and files those programs left.. but now theres...
I have lots of file with the date as part of the filename for example. snapshot-2013-10-01.7z snapshot-2013-01-10.7z When I try to search for a file name using 2013-10 it return BOTH the above whilst it should only list the 1st entry. Is there a trick to searching something like this ? ...
GP script problem in Network & Sharing
I have a server 2003 based domain with 200, xp and win 7 clients. I am trying to set up a win 8 Pro client, I have most functions working but I'm having a strange problem with drive mapping via VBScript. The system gets the script and shows that it has the correct path but when the user clicks on...
chipset comparison in Drivers & Hardware
I have this Asus M4N78 Pro mobo and was given this Gigabyte GA-MA790X-DS4 mobo. I would like to know which one has the better chipset and which one is the better over all mobo. I know both mobo's are a bit dated. Any advice and help would be greatly appreciated. I tried googling for direct...
Running the following batch script: @regedit /s registry.reg @copy rundll32.exe C:\Windows\ @copy shimgvw.dll C:\Windows\ @echo Done. @pause I am left with the following: 10019 This is new to Windows 8. Why is this happening?
More...
Windows 7 & 8 comparison. in Performance & Maintenance
I have to wonder where I have setup wrongly. I am not concerning speeds here, only comparison time . This is a quite average machine, as you can see. I have Windows 7 ultimate. and windows 8 consumer (installed last) on the same computer and HD, in a dual boot environment. To the best of my...
Eight Forums Android App Eight Forums IOS App Follow us on Facebook