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

Hi,
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

 

LVL 1
MultimaticAsked:
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.

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

Of course you will need to install PowerShell and enable scripts.
Download Powershell
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx

Enable Scripts
http://myitforum.com/cs2/blogs/dhite/archive/2006/09/08/Configuring-PowerShell-Execution-Policies.aspx
# 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)
 $ZipFile.CopyHere($_.FullName)
 $_.Delete
 }

Open in new window

0
Shift-3Commented:
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
            'objFile.Delete
        End If
    End If
Next
 
Set colSubFolders = objRoot.SubFolders
 
For Each objFolder in colSubfolders
    GetSubFolders objFolder.Path
Next
 
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
                'objFile2.Delete
            End If
        End If
    Next
        
    Set colSubfolders2 = objSub.SubFolders
 
    For Each objFolder2 in colSubfolders2
        GetSubFolders objFolder2.Path
    Next
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))
        objTxt.Close
    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
    Loop
End Sub

Open in new window

0

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

Cheers!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.