Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Vbscript Compress Logs by Month

Posted on 2012-09-21
4
Medium Priority
?
535 Views
Last Modified: 2012-09-21
Hi,

Currently I have the following script to compress the log from one folder. I would like to update this script to read the strRoot from one .txt file with all locations that I would like to compress. How can I adapt the script? because I would like specify which folders I will compress with this script from a txt file.

This is the script:
strRoot = "F:\Logs\IIS"
strExt = "txt"
strExt1 = "log"
blnDeleteOriginals = True
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
DoFolder strRoot
 
Sub DoFolder(strFolder)
    Set objFolder = objFSO.GetFolder(strFolder)

 
    ' Process all files in this folder
    For Each objFile In objFolder.Files
        strFile = objFile.Path
        strFileExt = objFSO.GetExtensionName(strFile)
 
        If (LCase(strFileExt) = LCase(strExt)) Or (LCase(strFileExt) = LCase(strExt1)) Then
            dtmFile = objFile.DateLastModified
            strZIP = strFolder & "\" & MonthName(Month(dtmFile)) & Year(dtmFile) & ".zip"
            ZipFile strFile, strZip
 
            If blnDeleteOriginals and objFile.DateLastModified < (Date() - 1)  Then
                objFile.Delete True
            End If
        End If
    Next
 
    ' Process all subfolders in this folder
    Set objSubFolders = objFolder.SubFolders
    If objSubFolders.Count > 0 Then
        For Each objSubFolder in objSubFolders
            DoFolder objSubFolder.Path
        Next
    End If
 
End Sub
 
Sub ZipFile(strFileToZip, strArchive)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
 
    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
 
    WScript.Sleep 2000
    Set objApp = CreateObject("Shell.Application")
    intCount = objApp.NameSpace(strArchive).Items.Count + 1
    objApp.NameSpace(strArchive).CopyHere strFileToZip
 
    Do
        WScript.Sleep 200
        Set objNameSpace = objApp.NameSpace(strArchive)
 
        If Not objNameSpace Is nothing Then
            If objNameSpace.Items.Count = intCount Then
                Exit Do
            End If
        End If
    Loop
End Sub

Open in new window

0
Comment
Question by:Gonzalo Becerra
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 38422539
I think this should handle that, just update the path to the new txt file near the top.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

strInFile = "C:\temp\folders.txt"
strExt = "txt"
strExt1 = "log"
blnDeleteOriginals = True
 
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open input file
Set objInfile = objFSO.OpenTextFile(strInfile, ForReading, False, TriStateUseDefault)

' Read input file line by line
Do While Not objInfile.AtEndOfStream
   strRoot = objInfile.ReadLine
   DoFolder strRoot
Loop

' Cleanup and end
objInfile.Close

 
Sub DoFolder(strFolder)
    Set objFolder = objFSO.GetFolder(strFolder)
 
    ' Process all files in this folder
    For Each objFile In objFolder.Files
        strFile = objFile.Path
        strFileExt = objFSO.GetExtensionName(strFile)
 
        If (LCase(strFileExt) = LCase(strExt)) Or (LCase(strFileExt) = LCase(strExt1)) Then
            dtmFile = objFile.DateLastModified
            strZIP = strFolder & "\" & MonthName(Month(dtmFile)) & Year(dtmFile) & ".zip"
            ZipFile strFile, strZip
 
            If blnDeleteOriginals and objFile.DateLastModified < (Date() - 1)  Then
                objFile.Delete True
            End If
        End If
    Next
 
    ' Process all subfolders in this folder
    Set objSubFolders = objFolder.SubFolders
    If objSubFolders.Count > 0 Then
        For Each objSubFolder in objSubFolders
            DoFolder objSubFolder.Path
        Next
    End If
 
End Sub
 
Sub ZipFile(strFileToZip, strArchive)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
 
    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
 
    WScript.Sleep 2000
    Set objApp = CreateObject("Shell.Application")
    intCount = objApp.NameSpace(strArchive).Items.Count + 1
    objApp.NameSpace(strArchive).CopyHere strFileToZip
 
    Do
        WScript.Sleep 200
        Set objNameSpace = objApp.NameSpace(strArchive)
 
        If Not objNameSpace Is nothing Then
            If objNameSpace.Items.Count = intCount Then
                Exit Do
            End If
        End If
    Loop
End Sub

Open in new window

~bp
0
 
LVL 1

Author Comment

by:Gonzalo Becerra
ID: 38422583
Thank you, I think something is missing or not working I'm executing with results:

Result:
C:\Temp>cscript zip.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Temp>

Open in new window


The script is not doing the zip on the files I added the msgbox here and is reading ok the content on the folders.txt but I don't know is not compressing properly the files in the folder.

' Read input file line by line
Do While Not objInfile.AtEndOfStream
   strRoot = objInfile.ReadLine
   msgbox strroot
   DoFolder strRoot
Loop
0
 
LVL 1

Author Closing Comment

by:Gonzalo Becerra
ID: 38422601
Sorry is working perfect!! i miss to change the extension of the files to compress.


Thank you very much for your help!!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38422622
Great, glad that helped, thanks for the feedback.

~bp
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

580 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