Solved

VBScript or Batch file to compress log files and move to another drive

Posted on 2010-08-18
7
1,407 Views
Last Modified: 2012-05-10
I need a VBScript or Batch file to zip all the .log files in the various subfolders of %windows%\System32\LogFiles that are, say 14 days or older and move them to a different drive:\folder.

I found this thing  http://winadmin.forret.com/scripts/warmzip/
It's suppose to allow you to script compressing and moving files.  It looks very promising, but I got an error when I ran one of the sample batch files.

Thanks in advance.
0
Comment
Question by:megnin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Author Comment

by:megnin
ID: 33468899
I found this and modified the folder path to mine, but this script only does one folder.  I need to look in about 28 folders in the LogFiles folder and process each of them.  I haven't tested the code below either...
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' This script will find logs that are more than a day old and ''
'' compress them.                                              ''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
         
Dim objFSO, objFolder, objFiles, objShell
Dim file, fileExt, fileName, strCommand, strRun, strFile
Dim Folder, Extension, DaysOld, log
Dim strYear, strMonth, strDay, strHour, strMinute, strSecond 

 
Const ForReading = 1, ForWriting = 2, ForAppending = 8
 
         
'''' SET THESE VARIABLES! ''''
Folder = "C:\WINDOWS\system32\LogFiles"      'Folder to look in
Extension = "log"        'Extension of files you want to zip
DaysOld = 7        '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
 
'Set text file to write to
FileName = "C:\WINDOWS\system32\LogFiles\Log_Processing.txt"
 
Set log = objFSO.OpenTextFile(FileName, ForAppending)
 
         
'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
              strFile = file.Path
              strCommand = "7z -mx=7 -y a C:\WINDOWS\system32\LogFiles\web_log_compressed\" & _
                ShortDateTime(Now) & ".zip " & Chr(34) & strFile & Chr(34)
              strRun = objShell.Run(strCommand, 0, True)
              log.WriteLine strCommand
              file.Delete
              log.WriteLine "Deleted " & strFile
            End If
            
          End If
        Next
         
        'Cleanup
        Set objFiles = Nothing
        Set objFolder = Nothing
        Set objFSO = Nothing
        Set objShell = Nothing
        wscript.Quit
        
Function ShortDateTime(dtmTime)
    strYear = Year(dtmTime)
    strMonth = Right("0" & Month(dtmTime), 2)
    strDay = Right("0" & Day(dtmTime), 2)
    strHour = Right("0" & Hour(dtmTime), 2)
    strMinute = Right("0" & Minute(dtmTime), 2)
    strSecond = Right("0" & Second(dtmTime), 2)
    
    ShortDateTime = strYear & strMonth & strDay & "-" & strHour & strMinute & strSecond
End Function

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33469200
WHy not just use WinZip with the commandline version, and do it all with one neat little command?  Something like this should do the job:

wzzip -m -Td14 "d:\destfolder\destfile.zip" "%windows%\System32\LogFiles"

~bp
0
 
LVL 1

Author Comment

by:megnin
ID: 33469257
I guess that woud be the simplest way to do it.  There is really no good reason to do each folder separately.  I've never used the commandline version of WinZip.  If it will grab just the *.log files that are >14 days old from all the sub folders then that will work a charm.
Can you have it delete the source files after zipping them?  It doesn't help if they are left lying around.  I need to do this to free up hard drive space on a db server.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33469312
Yes, wzzip can delete the source files, I added the -m option to the command line (standing for move) that does exactly that.  And yes, it could handle just *.log files like:

wzzip -m -Td14 "d:\destfolder\destfile.zip" "%windows%\System32\LogFiles\*.log"

~bp
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 33469854
Thank you so much!  Now we can free up enough space on the system drive of our SQL Server to do an upgrade.
You just saved my company $15,000.00 by not having to buy a new server.  ;-)
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33470877
Excellent, glad I suggested that, and glad it worked out.  Thanks for the grade, when can I expect my cut of the savings... :-)

~bp
0
 
LVL 1

Author Comment

by:megnin
ID: 33474143
Post your bank account number and routing number here and I'll have it wired.  ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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