Backup - ZIP - Then email

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Backup - ZIP - Then email

Post by alanimal » Thu Oct 08, 2009 11:17 pm

Do you know if there is an easy way to automate a backup of certain files in a folder? What I need to do is, back up the last 31 files in a folder that have been modified by date.

All he files are named yyyymmdd.jou (year, month, day) these need to be zipped up and then emailed. Not asking too much??

Cheers in advance

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 09, 2009 8:29 am

Not asking much no. We can use GetFileList to get a list of the files. Then loop through them, zip them using the zip library and email them using SMTPSendMail.

However, the devil is in the detail. Before we go writing example code for you can you explain what you mean by the last 31 files? Do you mean the NEWEST 31 files, or the OLDEST 31 files? Is it always 31 or might it vary? Please describe the criteria that determines which files should be zipped up. Then we can give you some more specific help and maybe even write some code for you.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Sun Oct 11, 2009 11:23 pm

Thanks mate!
That figure has changed to 7 days, it should be the previous 7days. IE. If it was done today, the files I would need would have been modified on 05, 06,07,08,09,10,11 Oct.

8)

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 14, 2009 9:15 am

So you want a script that zips up the latest 7 files in a folder, and emails that zip?

Ok. First you need to download the Zip Library from:
http://www.mjtnet.com/plugins.htm

Open up the ziplib.zip file and extract ziplib.dll and ziplib.ini into the Imports folder in your Macro Scheduler program folder. If an Imports sub folder does not exist, create one. So this Imports folder should be beneath the main Macro Scheduler 11 program folder (usually c:\program files\macro scheduler 11).

Now create a new macro and paste in the following code:

Code: Select all

//Config
Let>file_folder=c:\users\user\documents
Let>zip_file=c:\users\user\documents\zips\myzip.zip
Let>server=myserver.com
Let>[email protected]
Let>from_name=myself
Let>[email protected]
Let>subject=Here's the zip file
Let>body=See attached

//Run a DOS cmd to get list of files in folder sorted by date (oldest first - dos only does oldest first)
Let>RP_WINDOWMODE=0
Let>RP_WAIT=2
Run>cmd.exe /c dir "%file_folder%" /B /A-D /OD >> "%TEMP_DIR%\files.txt"

//Read in files
ReadFile>%TEMP_DIR%\files.txt,file_list
DeleteFile>%TEMP_DIR%\files.txt

//Separate file_list to array
Separate>file_list,CRLF,files

//We want last 7 items
Let>k=files_count-7
If>k<1
  Let>k=1
Endif

//loop through and zip them up
Repeat>k
  Let>this_file=files_%k%
  //worth double checking file exists
  IfFileExists>%file_folder%\%this_file%
    ZIP_Add>%zip_file%,%file_folder%\%this_file%,9,0,r
  Endif
  Let>k=k+1
Until>k=files_count

//now we can email the zip file
SMTPSendMail>recipients,server,from_email,from_name,subject,body,zip_file
Modify the first 8 lines to suit your setup. The first is the path of the folder containing the files you want to zip up. The second is the path for the zip file you want to create/modify.

Hopefully the rest of the code is self-explanatory.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Wed Oct 14, 2009 11:18 pm

Thanks heaps for the code. The problem I have is that it is sending an email without an attachment - not sure what is the issue here - any ideas?
Your a legend thanks!

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Tue Oct 20, 2009 8:35 pm

hey mate - i have been trying to get this to work for me but have had no joy. I am receiving an email with no attachment - is there something I have missed? here is my code:

Code: Select all

Let>file_folder=c:\bup\
Let>zip_file=c:\zips\myzip.zip
Let>server=mail.chsglobal.com
Let>[email protected]
Let>from_name=ANZ_Backup
Let>[email protected]
Let>subject=Here's the zip file
Let>body=See attached

//Run a DOS cmd to get list of files in folder sorted by date (oldest first - dos only does oldest first)
Let>RP_WINDOWMODE=0
Let>RP_WAIT=2
Run>cmd.exe /c dir "%file_folder%" /B /A-D /OD >> "%TEMP_DIR%\files.txt"

//Read in files
ReadFile>%TEMP_DIR%\files.txt,file_list
DeleteFile>%TEMP_DIR%\files.txt

//Separate file_list to array
Separate>file_list,CRLF,files

//We want last 7 items
Let>k=files_count-7
If>k<1>k=1
Endif

//loop through and zip them up
Repeat>k
  Let>this_file=files_%k%
  //worth double checking file exists
  IfFileExists>%file_folder%\%this_file%
    ZIP_Add>%zip_file%,%file_folder%\%this_file%,9,0,r
  Endif
  Let>k=k+1
Until>k=files_count

//now we can email the zip file
SMTPSendMail>recipients,server,from_email,from_name,subject,body,zip_file

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Oct 20, 2009 9:41 pm

Is the zip file being created?

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Tue Oct 20, 2009 10:03 pm

nah - cant see it anywhere. perhaps its a prob with the temp folder???

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Oct 21, 2009 12:13 am

Step through it in the debugger and see where it fails.

Also I suspect it's the forum that has corrupted it but your posted code has

Code: Select all

//We want last 7 items
Let>k=files_count-7
If>k<1>k=1
Endif
which should be

Code: Select all

//We want last 7 items
Let>k=files_count-7
If>k<1
  Let>k=1
Endif

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 21, 2009 8:34 am

Let's start with the bleeding obvious. Did you download and install the zip library as described? To be sure: Do you see an Imports folder in c:\program files\Macro Scheduler 11 and does that Imports folder include ziplib.dll and ziplib.ini ?

Which version of Macro Scheduler are you using?

When you step through with the debugger do you see values for file_list and files in the watch list? What is the value of files_count? Do you step through the Repeat/Until and onto Zip_Add or does that bit get skipped?

C'mon - give us a small scrap to go on .... my crystal ball is out of magic dust.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Thu Oct 29, 2009 12:00 am

Thanks - I am using 7.4 - I guess this wont work with the zip library?

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Thu Oct 29, 2009 12:36 am

Ok, upgraded to 11 and it works a treat! Thanks heaps everyone!

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Sun Nov 01, 2009 11:35 pm

one further thing - is there any way to zip up one file type if there are multiple file types in the folder? There are .log and .jou files in the directory and we only want the .jou files. thanks!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Mon Nov 02, 2009 12:37 am

The best way to do that is to include the filespec when you define the source directory so:

Instead of:

Let>file_folder=c:\bup\

make it:

Let>file_folder=c:\bup\*.jou

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Mon Nov 02, 2009 1:12 am

thanks, tried that - now it sends the email without the attachment

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts