Compress Txt Files per month - Batch or Vbscript

Hi,
I need help to make a vbscript or batch file to compress all. txt, placing all. txt file the same in a month eg January2010. one with all the files in February2010

Directory:
D:\Data\SumTotal\data\lang-default\logs
LVL 1
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Based on what, the timestamp (last update?) of the file, or is the name of the txt file to be used for determining the month?

~bp
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
Based on "Last Modified"
Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable on line 1 with the location of the folder containing the files.  Running the script will zip all .txt files in the folder into archives named for the month and year.

To delete the original files after zipping them, set the blnDeleteOriginals variable on line 3 to True.  Use this with caution, as it will delete files permanently, without sending them to the Recycle Bin.

If you have already run the script with blnDeleteOriginals set to False, delete the existing .zip files before running the script again, as duplicate filenames within the archives will cause problems.


strRoot = "D:\Data\SumTotal\data\lang-default\logs"
strExt = "txt"
blnDeleteOriginals = False
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRoot = objFSO.GetFolder(strRoot)
 
For Each objFile In objRoot.Files
    strFile = objFile.Path
    strFileExt = objFSO.GetExtensionName(strFile)
    
    If LCase(strFileExt) = LCase(strExt) Then
        dtmFile = objFile.DateLastModified
        strZIP = strRoot & "\" & MonthName(Month(dtmFile)) &_
            Year(dtmFile) & ".zip"
        ZipFile strFile, strZip
        
        If blnDeleteOriginals Then
            objFile.Delete True
        End If
    End If
Next
 
Sub ZipFile(strFileToZip, strArchive)
    'Adapted from http://www.robvanderwoude.com/vbstech_files_zip.php
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
Thanks alot!  Work Fine :)
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
I can do as a recursive for all subfolders? can?
simonlimonCommented:
You can also use Winrar (shareware), which can do this out of the box and is much simpler than trying to write a script, a simple batch command would look like this:

"c:\tools\Rar.exe" a -r "source_directory" -ag[yyyy-mm-dd] -df -s -to7d  "destination_directory"
Bill PrewIT / Software Engineering ConsultantCommented:
==> simonlimon

I think there is a problem with that approach.  gbecerra needs the files placed in an archive that is named with the date that the file was last changed.  I think the "-ag" option names the archive based on the todays current date, not that of the file being added.

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
Here's a modified version of the accepted solution that should drill down into subfolders.  I wasn't sure where you wanted the files to go in the subfolders, into ZIPs in the subfolder, or into one set of ZIPs all kept at the base folder level.  This approach keeps the ZIPs in the subfolders.

~bp
strRoot = "D:\Data\SumTotal\data\lang-default\logs"
strExt = "txt"
blnDeleteOriginals = False
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
DoFolder strRoot
 
Sub DoFolder(strFolder)
    Set objFolder = fso.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) Then
            dtmFile = objFile.DateLastModified
            strZIP = strFolder & "\" & MonthName(Month(dtmFile)) & Year(dtmFile) & ".zip"
            ZipFile strFile, strZip
 
            If blnDeleteOriginals 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)
    'Adapted from http://www.robvanderwoude.com/vbstech_files_zip.php
    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

Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
billprew thanks for all
it would be best to do the same process to compress a month within each subfolder also.
i have this error.
processlog.vbs(9, 5) Microsoft VBScript runtime error: Object required: 'fso'
Bill PrewIT / Software Engineering ConsultantCommented:
Sorry about that, always the risk when cutting and pasting from prior solutions.  That line should be changed to:

Set objFolder = objFSO.GetFolder(strFolder)

~bp
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
Thanks alot!!!!  :) Work OK :)
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
I check the archives and accumulate more than 1 month per file.
Bill PrewIT / Software Engineering ConsultantCommented:
==> Thanks alot!!!!  :) Work OK :)

If that was for the sub folder processing addition then glad to hear that was helpful.

==> I check the archives and accumulate more than 1 month per file.

I think what you are saying is that say in January2010.zip you are seeing files with a last modified date of January 2010, but also files with last modified dates of different months?  Looking at the original code by Shift-3 I don't see an obvious problem, so want to make sure I understand what you are saying.

~bp
Gonzalo BecerraSharePoint - Technical Lead for Operations & Engineering Team - Superrvising AssociateAuthor Commented:
The script works OK bill ordering the archvo per month, but in the latter script that runs the folders accumulate more than 1 month per file. Sorry for the confusion
Bill PrewIT / Software Engineering ConsultantCommented:
Don't mean to be dense, but still not getting it.  Could you maybe give an example of a couple of files in a couple of subfolders and indicate the results and what is wrong.  I'm still not sure what you mean by "the folders accumulate more than 1 month per file".

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.