Please Squeeze the Files
For a lighter, roomier system, a little file compression goes a long way.
- By Jeffery Hicks
If you're like me, you still have a number of systems running that are more than a few years old. They're still viable, but they can frequently run low on disk space. Because I don't often have time or money to replace disks, I have to find other ways to save space -- or at least fool the operating system into thinking the situation isn't as dire as it really is.
I wrote a Windows PowerShell script that does the heavy lifting -- or compressing, as the case may be. The PowerShell script, SearchSqueeze.ps1, uses Windows Forms to create a graphical utility. It'll run on PowerShell versions 1 or 2. Once launched, everything else is handled in the form.
Under the hood, the script uses Windows Management Instrumentation (WMI) and the CIM_Datafile class to find files that meet defined criteria. The script will compress each matching file by invoking the class's Compress() method. The form's purpose is to collect the necessary information to find and compress the files. Think of the script as a PowerShell wizard.
Setting up the Script
First specify a drive or folder to search. I'm assuming the underlying volume is formatted as NT File System (NTFS). There's a check-box control you can use to indicate a recursive search.
By default, the script will compress all files in the specified folder or subfolders. Usually, though, you only want to compress certain file types. The form has a default list of file extensions I frequently compress. Check the box and edit the list to meet your needs. Don't include any periods or wildcards; what you want is a comma-separated list of extensions. You may only want to compress files that haven't been modified since a certain date. Use the DateTime picker object to select a date and check the corresponding box. You may want to compress files based on size. Check the size box and enter a value for the minimum file size in kilobytes. The beauty of the tool is that you can mix and match any or all of these criteria.
Personally, I like having a record of what happens on my system, so I've included a logging feature that writes results to a text file. If you click the appropriate box, a .CSV log file is created that will contain the full filename, its size, when it was last modified and the Compress() method's return value. You should get a value of zero. For anything else, click here to decipher the result value. Regardless of whether you enable logging, all compressed files will be listed in the form's text box.
The last feature I included was my "WhatIf" box. If you check the List Only box, the form will still list all files that meet your criteria but won't actually compress them. You can log the results to a .CSV file as well.
Once you've made your selections and click the Compress button, the script block tied to the button's OnClick event fires. The script block, $Verify, calls a function that displays a MsgBox verifying you want to carry out the operation. If you click OK, the Compress script block is called. In this script block, the WMI query is assembled based on what has been checked and the text-box values. Most of the query is displayed in the form's status bar, and filenames are displayed in the text box and compressed.
Beware of broadly searching for files under C:\Windows. Many folders are protected, causing the script to appear to hang for a while, though it should eventually keep going. The PowerShell script should be ready to run as is, but you can certainly modify it to meet your needs.
I'm not suggesting compression is an effective file-management technique -- but there may be times when squeezing a few files will make you feel better.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org.
Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)