We help IT Professionals succeed at work.

Help with a vbs file

QPR
QPR asked
on
402 Views
Last Modified: 2013-11-25
I have a script below that has a couple of problems.
What I am trying to do is to zip up log files (that are older than 5 days old) into a single zip.
Problems that I have are....
It reopens the zip file for each file. I'd like to open the program once and have it zip them all.
It zips them with their full file paths. I want 1.log, 2.log, 3.log etc but each one zips as folder\folder\folder\folder\folder\1.log

I thought if I did a dir > filelist.txt then I could just pass filelist.txt to 7za but how do I only DIR the files that are > 5 days old?
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
 '' This script will find logs that are more than X days old and '' 
 '' compress them.                                              '' 
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
 Option Explicit 
   
 Dim objFSO, objFolder, objFiles, objShell 
 Dim file, fileExt, fileName, strCommand, strRun, strFile 
 Dim Folder, Extension, DaysOld 
 Dim ZipFile
 
 ZipFile = Day(Date) & Month(Date) & Year(Date) & ".zip"
 
   
 '''' SET THESE VARIABLES! '''' 
 Folder = "\\servername\T1\F1\data\log\LogTester"      'Folder to look in 
 Extension = "log"        'Extension of files you want to zip 
 DaysOld = 5        'Zip files older than this many days 
 '''''''''''''''''''''''''''''' 
   
 'Create object for playing with files 
 Set objFSO = CreateObject("Scripting.FileSystemObject") 
   
 'Create shell object for running commands 
 Set objShell = wscript.createObject("wscript.shell") 
   
 'Set folder to look in 
 Set objFolder = objFSO.GetFolder(Folder) 
   
 'Get files in folder 
 Set objFiles = objFolder.Files 
   
 'Loop through the files 
 For Each file in objFiles 
   fileName = Split(file.Name, ".") 
   fileExt = fileName(UBound(fileName)) 
   'See if it is the type of file we are looking for 
   If fileExt = Extension Then 
     'See if the file is older than the days chosen above 
     If DateDiff("d", file.DateLastModified, Now()) >= DaysOld Then 
       'objFSO.MoveFile file, "\\servename\T1\F1\data\log\logtester\files2Zip\"
       strFile = file.path
      ' strCommand = "7za -mx=9 a \\servename\T1\F1\data\log\logtester\" & ZipFile & strFile 
      strCommand = "7za -mx=9 a " & ZipFile & strFile 
 
       strRun = objShell.Run(strCommand, 0, True) 
       'wscript.echo strCommand 
       file.Delete 
     End If 
      
   End If 
 Next 
   
	
 
       'wscript.echo "Deleted " & strFile 
 
 
 'Cleanup 
 Set objFiles = Nothing 
 Set objFolder = Nothing 
 Set objFSO = Nothing 
 Set objShell = Nothing 
 wscript.Quit

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
QPR

Author

Commented:
thanks, I'll check it out when back to work.
Out of curiousity, where is the bit that actually says I'm going to create a zipped file/archive?
All I see is CreateTextFile

Commented:
That's it.  A bare-bones zip archive is just a text file with those characters in it.  Once it's created you can copy files to it using the CopyHere method.

Note that this only works on Windows XP and newer, as 2000 didn't have built-in zip capability.
QPR

Author

Commented:
it has a compression ratio of 0%
the size of the zip file is the same as the collective size of the text files in it.

How can I compress?
QPR

Author

Commented:
cancel that, turns out the files I tested on were gobbldegook and not plain text. Test 2 with plain text files and it works perfectly.
One thing tho...
What does
If Not objFSO.FileExists(strArchive) Then
        Set objTxt = objFSO.CreateTextFile(strArchive)
        objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
        objTxt.Close
    End If

the objTxt.Write line doing?

Commented:
It's adding a series of ANSI characters to the file.  See these pages for more information:
http://www.w3schools.com/VBscript/func_chr.asp
http://www.w3schools.com/VBscript/func_string.asp
QPR

Author

Commented:
thanks but why/where?
I don't see "pk" anywhere in the title or the logs being zipped?

Commented:
The PK stands for Phil Katz who originally created the .zip format.  The string is the file's header, containing its magic number.
QPR

Author

Commented:
I see (I think :-)
Thanks for sticking with me and explaining... last thing then I'll leave you alone.
So, this info is necessary in the file header so that OS/programs know how to store the data?
Sorry, this is all new to me being a SQL person.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.