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

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


MultimaticTechnical Services ManagerAsked:
Who is Participating?
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.

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

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

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
MultimaticTechnical Services ManagerAuthor Commented:
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.  

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
System Utilities

From novice to tech pro — start learning today.