Solved

VBScript to Zip Up files with option to delete them

Posted on 2011-03-07
17
715 Views
Last Modified: 2013-11-25
I need help coming up with a visual basic script.  I have looked and found several close items but nothing exact and I do not know enough about vbs to write or adjust a script.  

I have a folder that will get a lot of xml, pdf and txt files.  I want to schedule a task to run this script once a month.  

I would like the script to have a variable amount of days old to allow it to zip up any files over the variables number of days old.  

I want to have a variable to turn on and off deleting the files once they have been zipped.  

I would also like the script to have a variable of the file location so that I can use this on more than one server.  

Finally I would also like the zip file created to be named the month that it runs.  

It will run on server 2003 or newer so I do not want to use winzip or winrar.

I hope that my explanation is ok.

Thanks
0
Comment
Question by:tdisalvo
  • 9
  • 8
17 Comments
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
I've got something that might do the job.

I've got a VBscript that will look at a folder and for each subfolder will zip up files over 7 days old, and delete any zip files over 60 days old.  The zip files are named [originalfilename]_yyyymmdd.zip and it zips using the built-in compressed folder API.  The folder, zip age and delete age can all be configured using command-line parameters and the script can spit out details of it's progress to screen and/or a log file. you can also get command-line help by running it with the /? switch.
Start-LogRotation.txt
It would be pretty easy to modify it to only select certain file extensions and only works on the specified folder, not the subfolders.  The frequency that it runs is up to the task scheduler. If you like it and need a minor tweak or two then let me know and I'll see what I can do.
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
Forgot to mention, save the .txt file and rename it to have a .vbs extension.
0
 

Author Comment

by:tdisalvo
Comment Utility
3 questions...1 will this zip up all of the files that it finds in the directory that are greater than the 7 day old period into 1 large zip or many small zips based on the date the file was created?  I am looking to zip up as much stuff as I can in a monthly zip file, so all of august,all of sept ect.  

2 will this delete the files that are being zipped up after they are zipped?

3 is the delete Zip archives that are older than 60 days mandatory?  We are looking to keep the Zips indefinitely.  The docs created are international shipping docs that we will need for many years.
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
1. It zips up each file individually.

2. Once the file has been successfully zipped, the original is deleted

3. Currently the script will delete after X days, with 60 being the default.  You could use the command-line parameter or change the default to 999999, which is effectively never, or even simply comment out that bit of code (10 lines)

It would take a little while to get it to add files to a monthly zip as that changes the logic somewhat.
0
 

Author Comment

by:tdisalvo
Comment Utility
That is truly the desired affect, this way we do not have tons of files to go though.  

I am not sure if you would be able to assist.  I do appreciate the example to start with either way.  

Thanks
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
OK, it's a bit rough and ready but this version will copy everything into a zip file named yyyy-mm.zip (to allow for easy sorting)  based on the date when the script runs (not the file modification or creation date).  It runs a little slowly as I've got to allow time for the file to compress, but there's no simple way to programatically determine when that's happened, so I just wait for a short time that's based on the original file size.

I've set the defaults to:
  * Default folder to process: D:\Logfiles
  * Compress age: 7 days
  * Delete age: 3655 days (10 years, ish)

Run it in a command window as follows
Cscript Start-FileArchive.vbs /Folder:(folderpath)

Open in new window


Use the /? switch to get help.  Use the /compressage:# and /deleteage:# switches to change the default values for that script run.

Use the /debug switch to get loads of output on screen to see what it's doing.

Hope that does the trick for you...

Russ
Start-FileArchive.txt
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
Was that script any good?
0
 

Author Comment

by:tdisalvo
Comment Utility
Sorry about the delay.  We had our solar winds server blow up and I have been rebuilding it.  I will test the script Friday.

Thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tdisalvo
Comment Utility
Script works prefectly.  It moves all of the files to the archive folder and zips them up.  Then it goes through and deletes anything in the archive folder older than the delete date.  

The one thing I have a concern about is will it delte the ZIP file once it gets past the delete date?  If so is there a way to adjust it so that it knows not to do this?  

I tried to make some sense of the script but it is well beyond me.  Sorry about the delay in getting back to you.  As always your help is apricated.  
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
Glad it's doing the trick.  I've tweaked it again so the intDeleteAge variable defaults to -1.  If the /DeleteAge parameter is not specified then the file delete will be skipped completely.

Help text output below:

 Log Archiving and Rotation Tool (version 1.0.0)
 ===============================================

 Description
   This script will perform standard log rotation tasks for all within the
   sub-folders of a given folder.  All files over a given age in each 
   subfolder will be zipped and moved to an 'Archive' folder created within
   each folder.  If the /DeleteAge parameter is specified then all files 
   within each 'Archive' folder over the specified age will be deleted.

 Syntax
   Cscript Start-FileArchive.vbs /Folder:(path)
               [/CompressAge: /DeleteAge: /log[:] /debug /?]

 Required Arguments
   [none]

 Optional arguments
   /Folder:      [String] Default = D:\LogFiles
                 The full path to the root folder containing the files
                 to archive
   /CompressAge: [Integer] Default = 7
                 The age (in days) before a file will be compressed and
                 archived
   /DeleteAge:   [Integer]
                 The age (in days) before a compressed file will be
                 deleted.  If not specified then no files will be
                 deleted no matter how old they are.
   /?            Show this guide
   /trace        Show script debugging information
   /debug        Show detailed script debugging information
   /log          Log debugging info to default file
   /log:#        Log debugging info to specified file

 Requirements
   -> Minimum Windows Script Host version = 5.1
      Current Windows Script Host version = 5.8

Open in new window


Hope the SolarWinds server is feeling better :)
0
 
LVL 3

Accepted Solution

by:
RussPitcher earned 500 total points
Comment Utility
Of course, it's a little more helpful if I attach the script!

Start-FileArchive.txt
0
 

Author Comment

by:tdisalvo
Comment Utility
Russ,

Will the delete factor skip over Zip files or will it delete them as well?  curious as to how this would work and if there was an option to skip a file type.  

Thanks
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
The delete function is set to delete files in the \Archive folder over the given age, although it now won't delete anything unless you tell it.  It doesn't take any notice of the file type though.
0
 

Author Comment

by:tdisalvo
Comment Utility
Russ,

Can the script be modified to add this?  The goal it to have this run once a month and zip up all of the uncompressed files and then delete all of the old files.  However if it deletes the old zip files then there is a problem since we will not be able to retain the files.  

Thanks again.
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
I'm a little confused as to what you want it to do really, as all the files in the folder will be zipped up, so there wouldn't be anything but zip files to delete.

Once it's a bit clearer what needs doing it would be possible to do this, and if you've got anyone with any scripting abilities you should be able to find out how to modify the script fairly quickly with a look through Microsoft's Script Centre,  but to be honest I don't have any time time at the moment as I'm moving house next week!

Russ
0
 

Author Closing Comment

by:tdisalvo
Comment Utility
I was unable to state my question clearly so there seems like a lot of back and forth, but that is all on me.
0
 

Author Comment

by:tdisalvo
Comment Utility
Russ,

Good luck with the move, I know that moving is a huge pain.  

I am going to take a look at Microsoft's Script Centre and see if I can figure this out.  

The issue is that as of now it moves all of the files into the archive folder and then adds each of the files into the zip file.  then it deletes the file.  But the next time I run the script, next month, it will do the same, creating a new zip, essentially nesting the Zip files into each other, and then deleting all of the files execpt for the most recent zip.  

What I was hoping for was that it would be able to know to skip any file with a file extension of .zip that was in the archive folder.  so that when the processing is done I will have in the archive folder a .zip file for each month that the archiving occurs.  one for Jan, Feb, March, Ect.  

Thanks again for giving me the great head start on this.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now