mailsend script for FreeFileSync

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
eahm
Posts: 34
Joined: 14 Apr 2014 12:49

mailsend script for FreeFileSync

#1 Post by eahm » 14 Apr 2014 13:01

Hello guys,
I modified a script I found online that allows me to send logs via email when a backup is succeeded or failed.

The script works "almost" perfectly. There is a particular occasion when a log is reported as "Completed successfully", attached in the right way but the subject of the email says "Failed".

Now, I would like to completely change the script, I want to setup two variables, one for success text inside the log files and another for error text inside the same log files.

Here is the script:

Code: Select all

@echo off
timeout 1
for /f “delims=” %%x in (‘dir “C:\FFS\Logs\” /od /b’) do set recent=%%x
findstr /m /C:”successfully” “C:\FFS\Logs\%recent%”
findstr /m /C:”warnings” “C:\FFS\Logs\%recent%”
findstr /m /C:”errors” “C:\FFS\Logs\%recent%”
findstr /m /C:”aborted” “C:\FFS\Logs\%recent%”
findstr /m /C:”Nothing to synchronize” “C:\FFS\Logs\%recent%”
if %errorlevel%==0 (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Success] Synchronization completed successfully” -attach “C:\FFS\Logs\%recent%”
)
if errorlevel 1 (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Failed] Synchronization completed with errors” -attach “C:\FFS\Logs\%recent%”
)


Can we change it with something like:

Code: Select all

@echo off
timeout 1
for /f “delims=” %%x in (‘dir “C:\FFS\Logs\” /od /b’) do set recent=%%x
findstr /m /C:”successfully” “C:\FFS\Logs\%recent%” [b]VARIABLE SUCCESS[/b]
findstr /m /C:”warnings” “C:\FFS\Logs\%recent%” [b]VARIABLE FAILED[/b]
findstr /m /C:”errors” “C:\FFS\Logs\%recent%” VARIABLE FAILED
findstr /m /C:”aborted” “C:\FFS\Logs\%recent%” [b]VARIABLE FAILED[/b]
findstr /m /C:”Nothing to synchronize” “C:\FFS\Logs\%recent%” [b]VARIABLE SUCCESS[/b]
if [b]VARIABLE SUCCESS[/b] (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Success] Synchronization completed successfully” -attach “C:\FFS\Logs\%recent%”
)
if [b]VARIABLE FAILED[/b] (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Failed] Synchronization completed with errors” -attach “C:\FFS\Logs\%recent%”
)



Thank you.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: mailsend script for FreeFileSync

#2 Post by Squashman » 14 Apr 2014 13:11

Well you could do something like this but I have seen logs where there was success and failure in the same log so your results may still not come out correctly.

Code: Select all

findstr /m /C:”successfully” “C:\FFS\Logs\%recent%” && SET VARIABLE=SUCCESS
findstr /m /C:”warnings” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”errors” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”aborted” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”Nothing to synchronize” “C:\FFS\Logs\%recent%”  && SET VARIABLE=SUCCESS

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#3 Post by eahm » 14 Apr 2014 13:18

Should the full code be like this (sorry I really started playing with this stuff yesterday after 20 years I wasn't using it):

Code: Select all

@echo off
timeout 1
for /f “delims=” %%x in (‘dir “C:\FFS\Logs\” /od /b’) do set recent=%%x
findstr /m /C:”successfully” “C:\FFS\Logs\%recent%” && SET VARIABLE=SUCCESS
findstr /m /C:”warnings” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”errors” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”aborted” “C:\FFS\Logs\%recent%”  && SET VARIABLE=FAILED
findstr /m /C:”Nothing to synchronize” “C:\FFS\Logs\%recent%”  && SET VARIABLE=SUCCESS
if %success% (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Success] Synchronization completed successfully” -attach “C:\FFS\Logs\%recent%”
)
if %failed% (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Failed] Synchronization completed with errors” -attach “C:\FFS\Logs\%recent%”
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: mailsend script for FreeFileSync

#4 Post by Squashman » 14 Apr 2014 13:23

No. The IF statement doesn't work that way. You have to compare the string to another string.

IF "%variable%"=="SUCCESS" ......

IF "%variable%"=="FAILED" ......

Open up a command prompt and read the help for the IF command.

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

H:\>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT               Specifies that Windows should carry out
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.

  string1==string2  Specifies a true condition if the specified text strings
                    match.

  EXIST filename    Specifies a true condition if the specified filename
                    exists.

  command           Specifies the command to carry out if the condition is
                    met.  Command can be followed by ELSE command which
                    will execute the command after the ELSE keyword if the
                    specified condition is FALSE

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

The following would work if you want it all on one line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing

If Command Extensions are enabled IF changes as follows:

    IF [/I] string1 compare-op string2 command
    IF CMDEXTVERSION number command
    IF DEFINED variable command

where compare-op may be one of:

    EQU - equal
    NEQ - not equal
    LSS - less than
    LEQ - less than or equal
    GTR - greater than
    GEQ - greater than or equal

and the /I switch, if specified, says to do case insensitive string
compares.  The /I switch can also be used on the string1==string2 form
of IF.  These comparisons are generic, in that if both string1 and
string2 are both comprised of all numeric digits, then the strings are
converted to numbers and a numeric comparison is performed.

The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is
comparing against an internal version number associated with the Command
Extensions.  The first version is 1.  It will be incremented by one when
significant enhancements are added to the Command Extensions.
CMDEXTVERSION conditional is never true when Command Extensions are
disabled.

The DEFINED conditional works just like EXIST except it takes an
environment variable name and returns true if the environment variable
is defined.

%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead.  After running a program, the following
illustrates ERRORLEVEL use:

    goto answer%ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1

You can also use numerical comparisons above:

    IF %ERRORLEVEL% LEQ 1 goto okay

%CMDCMDLINE% will expand into the original command line passed to
CMD.EXE prior to any processing by CMD.EXE, provided that there is not
already an environment variable with the name CMDCMDLINE, in which case
you will get its value instead.

%CMDEXTVERSION% will expand into a string representation of the
current value of CMDEXTVERSION, provided that there is not already
an environment variable with the name CMDEXTVERSION, in which case you
will get its value instead.

H:\>

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#5 Post by eahm » 14 Apr 2014 13:29

Thank you again for the reply. This doesn't seem to work thought, I think I am missing something:

Code: Select all

@echo off
timeout 1
for /f “delims=” %%x in (‘dir “C:\FFS\Logs\” /od /b’) do set recent=%%x
findstr /m /C:”successfully” “C:\FFS\Logs\%recent%” && set variable=success
findstr /m /C:”warnings” “C:\FFS\Logs\%recent%”  && set variable=failed
findstr /m /C:”errors” “C:\FFS\Logs\%recent%”  && set variable=failed
findstr /m /C:”aborted” “C:\FFS\Logs\%recent%”  && set variable=failed
findstr /m /C:”Nothing to synchronize” “C:\FFS\Logs\%recent%”  && set variable=success
if "%variable%"="success" (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Success] Synchronization completed successfully” -attach “C:\FFS\Logs\%recent%”
)
if "%variable%"="failed" (
“C:\FFS\mailsend.exe” +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub “FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Failed] Synchronization completed with errors” -attach “C:\FFS\Logs\%recent%”
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: mailsend script for FreeFileSync

#6 Post by Squashman » 14 Apr 2014 13:35

Sorry. My a fault.

TWO ==

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#7 Post by eahm » 14 Apr 2014 13:41

Emails are not sending, how can I tell to stay open (to the .bat file) temporarily so I can read the error message?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: mailsend script for FreeFileSync

#8 Post by Squashman » 14 Apr 2014 13:47

pause

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#9 Post by eahm » 14 Apr 2014 13:50

Wow sorry man.

Here is the message:

Waiting for 0 seconds, press a key to continue ...
FINDSTR: Cannot open "C:\FFS\Logs\C-QBCompanies_NAS1-QBCompanies_Hourly
FINDSTR: Cannot open 2014-04-14
FINDSTR: Cannot open 123747.log"
FINDSTR: Cannot open "C:\FFS\Logs\C-QBCompanies_NAS1-QBCompanies_Hourly
FINDSTR: Cannot open 2014-04-14
FINDSTR: Cannot open 123747.log"
FINDSTR: Cannot open "C:\FFS\Logs\C-QBCompanies_NAS1-QBCompanies_Hourly
FINDSTR: Cannot open 2014-04-14
FINDSTR: Cannot open 123747.log"
FINDSTR: Cannot open "C:\FFS\Logs\C-QBCompanies_NAS1-QBCompanies_Hourly
FINDSTR: Cannot open 2014-04-14
FINDSTR: Cannot open 123747.log"
FINDSTR: Cannot open to
FINDSTR: Cannot open synchronize"
FINDSTR: Cannot open "C:\FFS\Logs\C-QBCompanies_NAS1-QBCompanies_Hourly
FINDSTR: Cannot open 2014-04-14
FINDSTR: Cannot open 123747.log"
Press any key to continue . . .



This is another thing I don't understand, from FreeFileSync help file:

Return Codes
0 - Synchronization completed successfully
1 - Synchronization completed with warnings
2 - Synchronization completed with errors
3 - Synchronization was aborted

According to those codes, shouldn't my first modified version JUST WORK?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: mailsend script for FreeFileSync

#10 Post by penpen » 14 Apr 2014 15:45

You are the wrong quotes:
Instead of the opening (hex of “: 0x93) and closing quotes (hex of ”: 0x94) you should use the default doublequotes (hex of ": 0x22).

penpen

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#11 Post by eahm » 14 Apr 2014 15:48

penpen wrote:You are the wrong quotes:
Instead of the opening (hex of “: 0x93) and closing quotes (hex of ”: 0x94) you should use the default doublequotes (hex of ": 0x22).

penpen

Got it, does that make the code not working? I copied part of the code (actually the first two "wrong" lines were copied), I only use 0x22, always.

Test:

Code: Select all

@echo off
timeout 1
for /f "delims=" %%x in ('dir "C:\FFS\Logs\" /od /b') do set recent=%%x
findstr /m /C:"successfully" "C:\FFS\Logs\%recent%"
findstr /m /C:"warnings" "C:\FFS\Logs\%recent%"
findstr /m /C:"errors" "C:\FFS\Logs\%recent%"
findstr /m /C:"aborted" "C:\FFS\Logs\%recent%"
findstr /m /C:"Nothing to synchronize" "C:\FFS\Logs\%recent%"
if %errorlevel%==0 (
"C:\FFS\mailsend.exe" +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub "FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% - [Success] Synchronization completed successfully" -attach "C:\FFS\Logs\%recent%"
)
if errorlevel 1 (
"C:\FFS\mailsend.exe" +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub "FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% - [Failed] Synchronization completed with errors" -attach "C:\FFS\Logs\%recent%"
)


Based on FreeFileSync's return codes numbers this script should work right?
Last edited by eahm on 14 Apr 2014 16:08, edited 5 times in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: mailsend script for FreeFileSync

#12 Post by penpen » 14 Apr 2014 15:58

Nearly in every line; same for the ‘’ in the for loop, use ' instead :

Code: Select all

@echo off
timeout 1
for /f "delims=" %%x in ('dir "C:\FFS\Logs\" /od /b') do set recent=%%x
findstr /m /C:"successfully" "C:\FFS\Logs\%recent%" && set variable=success
findstr /m /C:"warnings" "C:\FFS\Logs\%recent%"  && set variable=failed
findstr /m /C:"errors" "C:\FFS\Logs\%recent%"  && set variable=failed
findstr /m /C:"aborted" "C:\FFS\Logs\%recent%"  && set variable=failed
findstr /m /C:"Nothing to synchronize" "C:\FFS\Logs\%recent%"  && set variable=success
if "%variable%" == "success" (
"C:\FFS\mailsend.exe" +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub "FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Success] Synchronization completed successfully" -attach "C:\FFS\Logs\%recent%"
)
if "%variable%" == "failed" (
"C:\FFS\mailsend.exe" +bc +cc -v -smtp smtp.gmail.com -ssl -port 465 -auth -from 1STEMAIL -to 1STEMAIL -cc 2NDEMAIL -user 1STEMAIL -pass PASSWORD -sub "FFS_COMPANYNAME/PCNAME-BACKUPNAME: %time%, %date% – [Failed] Synchronization completed with errors" -attach "C:\FFS\Logs\%recent%"
)
I hope i've not overseen some of these characters (actually cannot test it: I'm posting using a mobile phone).

I'm not sure, but may it be, that your pc "want's" to behave "smart" and change your default (double)quotes by opening and closing (duble)quotes (I heard this could be enabled when using windows vista and higher)?

penpen

Edited: Added "smart" line.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: mailsend script for FreeFileSync

#13 Post by penpen » 14 Apr 2014 16:11

eahm wrote:Got it, does that make the code not working?
Yes, the special opening and closing quotes are not recognized as (default) quotes.

eahm wrote:Based on FreeFileSync's return codes numbers this script should work right?
Actually i cannot check it (mobile phone).
But the errorlevel you are checking only is affected by the last (in this case: findstr) command (it doesn't accumulate over all):

Code: Select all

if %errorlevel%==0 (
if errorlevel 1 (

penpen

Edit: Added the errorlevel part.
Last edited by penpen on 14 Apr 2014 16:17, edited 1 time in total.

eahm
Posts: 34
Joined: 14 Apr 2014 12:49

Re: mailsend script for FreeFileSync

#14 Post by eahm » 14 Apr 2014 16:16

Still [Failed]. This is insane.

Here is the subject of the email:

Code: Select all

FFS_COMPANYNAME/QBRemote-C-QBCompanies_NAS1-QBCompanies_Hourly: 15:13:10.21, Mon 04/14/2014 - [Failed] Synchronization completed with errors


Here is the content of the log:

Code: Select all

__________________________________________________________________________________________
|4/14/2014 - C-QBCompanies_NAS1-QBCompanies_Hourly: Synchronization completed successfully
|
|    Items processed: 8 (435 MB)
|    Total time: 00:00:11
|_________________________________________________________________________________________

[3:12:58 PM] Info: Starting comparison
[3:12:58 PM] Info: Synchronizing folder pair:
                       C:\QuickBooks Companies\
                       \\nas1\QBRemote\QBCompanies\
[3:12:58 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY1.QBW"
[3:12:59 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY1.QBW.ND"
[3:12:59 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY2.QBW"
[3:13:05 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY2.QBW.DSN"
[3:13:05 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY2.QBW.ND"
[3:13:05 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY2.QBW.TLG"
[3:13:06 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY1.QBW.DSN"
[3:13:06 PM] Info: Overwriting file "\\nas1\QBRemote\QBCompanies\COMPANY1.QBW.TLG"
[3:13:09 PM] Info: Synchronization completed successfully


penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: mailsend script for FreeFileSync

#15 Post by penpen » 14 Apr 2014 16:18

I've edited my above post too late, it seems: Please reread it.

penpen

Post Reply