mailsend script for FreeFileSync

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: mailsend script for FreeFileSync

#16 Post by penpen » 14 Apr 2014 16:29

In addition you should initialize the environment variable "variable" with an initializing value, before assigning "failed" and "success", and check if this initial value is still there.
Maybe:

Code: Select all

@echo off
set "variable=uninitialized"
:: (...)
if "%variable%" == "uninitialized" (
   echo error: variable still uninitialized.
) else if (... other cases)

You could also check if a "success" or "failed" value is overwritten:

Code: Select all

findstr (...) && if "%variable%" == "uninitialized" ( set "variable=success" ) else echo initialized variable (%variable%) should be overwritten in find number 1|...|5
This way you can see if the algorithm does what you expect, and if not you see, what is going wrong.

penpen

Edit1&2: Removed some flaws.
Last edited by penpen on 14 Apr 2014 16:33, edited 2 times in total.

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

Re: mailsend script for FreeFileSync

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

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


So, what you are saying is that only this line (findstr /m /C:"Nothing to synchronize" "C:\FFS\Logs\%recent%") is checked? Now I don't remember exactly but I'm sure it worked...maybe just the Subject? Damn it I have to retest all of it.

Should I need all of these lines just for the logs or the return codes are enough to send the right log?

Thanks.

edit:
This is getting way too much for my knowledge, if I see the full code I may understand it.

Can I make a small donation and you write the full thing? I don't know $50-100?

Also, I don't know how many possible lines there are, I just want to say: if you see "Completed successfully or "Nothing synchronized" send [Success], all the rest: send [Failed]. That's it. Or maybe playing based on the return codes? Don't know which is the best option.

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

Re: mailsend script for FreeFileSync

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

What might happen, if the following code is executed:

Code: Select all

findstr /m /C:"Nothing to synchronize" "C:\FFS\Logs\%recent%"
if %errorlevel%==0 (
"C:\FFS\mailsend.exe" (...)
)
if errorlevel 1 (
:: (...)
)

The findstr command sets the errorlevel.
The first if statement then checks if the errorlevel findstr has set:
If the errorlevel is zero, then "C:\FFS\mailsend.exe" (...) is executed and another errorlevel will be set.
The second if statement then checks the actual errorlevel (maybe set up by findstr or mailsend.exe depends on the actual case), and if it is one or higher then :: (...) will be performed.

So these are some possible cases:
- findstr sets the errorlevel to 1 => first if doesn't do anything, but the second.
- findstr sets the errorlevel to 1 => first if executes mailsend.exe, which sets the errorlevel to 1 => the second if statement executes :: (...)

penpen

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

Re: mailsend script for FreeFileSync

#19 Post by penpen » 14 Apr 2014 16:48

eahm wrote:Can I make a small donation and you write the full thing? I don't know $50-100?
Just post in detail, what you want to do, and i will try to post an code that works for free.

penpen

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

Re: mailsend script for FreeFileSync

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

This is what I want to do:
- Configure Mirror left to right with FreeFileSync (let's call it FFS)
- Save the batch file with the option to create logs on C:\FFS\Logs and launch mailalert.bat after the job is done
- Configure Windows Schedule Tasks to launch FFS.exe + Batch file as argument on daily/hourly basis (whatever the customer wants)
- After the backup/sync/mirror is done launch mailalert.bat

mailalert.bat will:
- Check C:\FFS\Logs folder for new log
- Read log OR check return code? (don't really know which option is better) (if second: code 0 = success, anything higher than 0 = error)
- Error? Send [Failed] email (always, on every error code inside the log possible)
- Success? [Send [Success] email (always, on every success code inside the log possible)

I think that's it.

The software is portable on C:\FFS

The logs are on C:\FFS\Logs

The batch jobs are on C:\FFS\Jobs

mailsend.exe is on C:\FFS

mailalert.bat is on C:\FFS

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

Re: mailsend script for FreeFileSync

#21 Post by Squashman » 14 Apr 2014 17:03

If your file sync is being launched from the batch file and is returning those error codes when it completes then just use the error codes instead of screwing around with checking the log.

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

Re: mailsend script for FreeFileSync

#22 Post by penpen » 14 Apr 2014 17:07

If i see it right, your initial post only does this (So i assume this is the "mailalert.bat"?):
- Check C:\FFS\Logs folder for the newest log (there is no check that this is not processed, so the newest log may be old[== processed earlier])
eahm wrote:- Read log
- Error? Send [Failed] email (always, on every error code possible)
- Success? [Send [Success] email (always, on every success code possible)
But i'm not sure if the logs may contain multiple events (success, warning, error, ...) and you haven't described, how to react in such a case.
Maybe there is a sort of order what might be "ignored".
For example if error > warning > success, and an error occurs, then the error is reported only all other events are ignored.

Maybe you test this first (it is your script with the 2 extensions above and using the environment variable "variable" content to react.

Code: Select all

@echo off
timeout 1
set "variable=uninitialized"

for /f "delims=" %%x in ('dir "C:\FFS\Logs\" /od /b') do set recent=%%x

findstr /m /C:"successfully" "C:\FFS\Logs\%recent%" && if "%variable%" == "uninitialized" ( set "variable=success" ) else echo initialized variable ^(%variable%^) should be overwritten in find number 1
findstr /m /C:"warnings" "C:\FFS\Logs\%recent%"     && if "%variable%" == "uninitialized" ( set "variable=failed" ) else echo initialized variable ^(%variable%^) should be overwritten in find number 2
findstr /m /C:"errors" "C:\FFS\Logs\%recent%"       && if "%variable%" == "uninitialized" ( set "variable=failed" ) else echo initialized variable ^(%variable%^) should be overwritten in find number 3
findstr /m /C:"aborted" "C:\FFS\Logs\%recent%"      && if "%variable%" == "uninitialized" ( set "variable=failed" ) else echo initialized variable ^(%variable%^) should be overwritten in find number 4
findstr /m /C:"Nothing to synchronize" "C:\FFS\Logs\%recent%"  && if "%variable%" == "uninitialized" ( set "variable=success" ) else echo initialized variable ^(%variable%^) should be overwritten in find number 5

if "%variable%" == "uninitialized" (
   echo error: variable still uninitialized.
) else 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%"
) else 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%"
) else (
   echo unexpected variable value ^(%variable%^).
)
If it does not what you want to, then just say what you see, why this is wrong, and what you want it to do instead.
(Sorry it's getting late, i go to bed now, i'll post tomorrow again.)

penpen

Edit: I've forgotten to escape the ) in echo's; corrected it; i also escape the ('s although not needed for better reading.

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

Re: mailsend script for FreeFileSync

#23 Post by eahm » 14 Apr 2014 17:20

Squashman wrote:If your file sync is being launched from the batch file and is returning those error codes when it completes then just use the error codes instead of screwing around with checking the log.

That would be actually amazing, because I don't really know how many options there are that the software writes inside the logs, I know 4-5 of them only because I saw them and that's it.

penpen, thanks for the code, however, it doesn't send any email. Sorry.

Wouldn't be easier to say return code = 0 send success, return code = >0 send failed ?

I tried this configuration because I don't think "Nothing to synchronize" reports any return code, not even 0? Not sure, I'd like to know even if there is nothing to sync, I want every single log via email.

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

Re: mailsend script for FreeFileSync

#24 Post by eahm » 14 Apr 2014 17:48

Am I crazy? This seems to be working perfectly:

Code: Select all

@echo off
timeout 1
for /f "delims=" %%x in ('dir "C:\FFS\Logs\" /od /b') do set recent=%%x
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%"
)


edit:
It doesn't, it didn't attach the latest Failed log when I changed the address of the NAS to make it not working. I need to fix ONLY this now.

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

Re: mailsend script for FreeFileSync

#25 Post by Squashman » 14 Apr 2014 19:19

I think you are not showing us something. How are you launching the File sync program. I assumed you were launching it from the batch file but I never see it in your code.

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

Re: mailsend script for FreeFileSync

#26 Post by eahm » 14 Apr 2014 19:21

I do it manually to test the backup then when everything works it will run automatically from the Windows schedule manager.

This code is inside mailalert.bat, the FFS batch file (.ffs_batch) saved by FreeFileSync tells FreeFileSync that when the job is done it needs to launch mailalert.bat to complete everything.

Code: Select all

<OnCompletion>C:\FFS\mailalert.bat</OnCompletion>


The last script is almost complete, the only problem is that now every log, even failed ones, sends as [Success].
Last edited by eahm on 14 Apr 2014 19:27, edited 4 times in total.

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

Re: mailsend script for FreeFileSync

#27 Post by Squashman » 14 Apr 2014 19:24

eahm wrote:I do it manually to test the backup then when everything works it will run automatically from the Windows schedule manager.

Then there was no point in ever mentioning the errorlevels coming from your filesync program.

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

Re: mailsend script for FreeFileSync

#28 Post by eahm » 14 Apr 2014 19:25

Squashman wrote:
eahm wrote:I do it manually to test the backup then when everything works it will run automatically from the Windows schedule manager.

Then there was no point in ever mentioning the errorlevels coming from your filesync program.

What do you mean? I launch the very same ffs_batch file launched in the automatic backup, it's a manual run but it's the same one. The script is tested as well.

I'm really tired, worked all day on four customers etc. I just need to fix this. Is there a way to send the correct email with my last script?

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

Re: mailsend script for FreeFileSync

#29 Post by Squashman » 14 Apr 2014 19:31

Is the file sync being launched from a batch file. If so why dont you put all your code into one batch file so that you can use the errorlevels from the file sync program.

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

Re: mailsend script for FreeFileSync

#30 Post by eahm » 14 Apr 2014 19:34

They are two different kind of batch files, one must be launched from FreeFileSync called batch file but is an xml file with all the configurations inside, its extension is .ffs_batch.

The other batch file, launched when .ffs_batch is done with the backup run, is the .bat I need to get to work. It's made ONLY for mailsend.exe, ONLY to try to read and send the logs via email when the backup run is done and when .ffs_batch calls it.

Apologize if this wasn't clear from the beginning, I assumed you all know FreeFileSync.

This is how it works:

FreeFileSync.exe (main program, can run even manually, itself) ---> Saved .ffs_batch (with all the configuration of the folders, when and where about saving log files, what to do on completion, even launch .bat files) ---> mailalert.bat (instructions for emailing logs) ---> mailsend.exe (only needed by mailalert.bat, smtp client).

edit:
I tried to let it scan every log but I have the same result, all logs are sent as [Success], even failed ones.

This is the code I used last

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:"processed" "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%"
)

The word "processed" is on every log.

I give up until someone helps or/and wait for penpen to wake up.


penpen or whoever has 5 minutes, to make it easier, this is what I need:

Return code 0? Attach last logfile.log with subject [Success]

Return code different than 0? Attach last logfile.log with subject [Failed]

That's it.

Thanks.

Post Reply