How do I batch zip files with .igs extension and retain original name and location then delete the .igs files

Posted on 2008-11-17
Last Modified: 2013-12-01
I am trying to perform the following:
1. Search files server for .igs and .stl files over 1 month old
2. Create a zip of ech file seperatley, i.e. C:\abc.igs beocmes C:\ and 123.igs becomes
3. delete orignal .igs and .stl files over 1 month old

I did find an excellent vbscript on the the exchange but it zipped all files to a single zip archive.

Many thanks


Question by:Multimatic
    LVL 1

    Expert Comment

    I think the following powershell script will do what you need.

    Of course you will need to install PowerShell and enable scripts.
    Download Powershell

    Enable Scripts
    # Set a cut-off date
    # We want to archive files more than a month old - I'll call that 30 days
    $Today = Get-Date
    $CutOffDate = $Today.AddDays(-30)
    # now find all the files on C: that have an extension of igs
    # and are older than 30 days
    $Files = Get-ChildItem C:\ -filter *.igs -recurse | where {$_.LastWriteTime -lt $CutOffDate}
    # Now zip each file and delete the original
    $Files | foreach {
     $ZipPath = $_.Directoryname
     $ZipName = [System.IO.Path]::GetFileNameWithoutExtension($_)
     $FullZipTarget = $ZipPath + "\" + $ZipName + ".zip"
     $ZipFile = (New-Object -ComObject Shell.Application).NameSpace($FullZipTarget)

    Open in new window

    LVL 38

    Accepted Solution

    Here's a way to do it with vbscript.

    Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable with the location of the folder to search under.  Running the script will zip the files.  Once you've tested it successfully, uncomment lines 13 and 34 to delete the original files.

    strRoot = "c:\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objRoot = objFSO.GetFolder(strRoot)
    Set colFiles = objRoot.Files
    For Each objFile in colFiles
        If LCase(Right(objFile.Name, 4)) = ".igs" Or LCase(Right(objFile.Name, 4)) = ".stl" Then
            If DateDiff("m", objFile.DateLastModified, Now) > 0 Then
                strNewPath = Left(objFile.Path, Len(objFile.Path) - 4) & ".zip"
                ZipFile objFile.Path, strNewPath
            End If
        End If
    Set colSubFolders = objRoot.SubFolders
    For Each objFolder in colSubfolders
        GetSubFolders objFolder.Path
    Sub GetSubFolders(strFolderPath)
        Set objSub = objFSO.GetFolder(strFolderPath)
        Set colFiles2 = objSub.Files
        For Each objFile2 in colFiles2
            If LCase(Right(objFile2.Name, 4)) = ".igs" Or LCase(Right(objFile2.Name, 4)) = ".stl" Then
                If DateDiff("m", objFile2.DateLastModified, Now) > 0 Then
                    strNewPath = Left(objFile2.Path, Len(objFile2.Path) - 4) & ".zip"
                    ZipFile objFile2.Path, strNewPath
                End If
            End If
        Set colSubfolders2 = objSub.SubFolders
        For Each objFolder2 in colSubfolders2
            GetSubFolders objFolder2.Path
    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))
        End If
        Set objApp = CreateObject( "Shell.Application" )
        intCount = objApp.NameSpace(strArchive).Items.Count + 1
        objApp.NameSpace(strArchive).CopyHere strFileToZip
        Do Until objApp.NameSpace(strArchive).Items.Count = intCount
            WScript.Sleep 200
    End Sub

    Open in new window

    LVL 1

    Author Closing Comment

    Excellent, works a treat thanks!  

    Just a liitle sidenote on how this solution would have been 'totally' great instead of just really great...  For VB dumbo's like me it would be great if there was a line of easy to understand instructiosn above any lines of code that one needs to change.  In this case the dates.  


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now