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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

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:\abc.zip and 123.igs becomes 123.zip....
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


1 Solution
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

MultimaticAuthor 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.  


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now