Link to home
Start Free TrialLog in
Avatar of Gonzalo Becerra
Gonzalo BecerraFlag for Argentina

asked on

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
Avatar of Bill Prew
Bill Prew

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
Avatar of Gonzalo Becerra

ASKER

Based on "Last Modified"
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot!  Work Fine :)
I can do as a recursive for all subfolders? can?
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"
==> 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
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

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'
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
Thanks alot!!!!  :) Work OK :)
I check the archives and accumulate more than 1 month per file.
==> 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
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
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