.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Articles >> PowerShell >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Technique: Compressing Files in PowerShell

Posted By:Manning       Posted Date: May 01, 2011    Points: 200    Category: PowerShell    URL: http://www.dotnetspark.com  

In this technique author explains how to use file compression to reduce the file size and ultimately save up to 80 to 90 percent of the space a file occupies.

The amount of data stored by organizations is continually growing. In this technique from PowerShell and WMI, author Richard Siddaway explains how to use file compression to reduce the file size and ultimately save up to 80 to 90 percent of the space a file occupies.

40% off any version of PowerShell and WMI with the checkout code dnspark40. Offer is only valid through www.manning.com.

The amount of data stored by organizations seems to be on a continuously upward spiral. Disk space is comparatively cheap so one strategy is to keep buying more disk space. This works for a while but, eventually, you run out of space in your SAN rack or data center. An alternative is to use file compression to reduce the size files occupy. This can save up to 80 to 90 percent of the space a file occupies depending on the file type.

Your file server drives are running out of space. There isn't any money in the budget to acquire extra storage. The larger files on the disk need to be compressed to reclaim some space.

The solution presented in listing 1 follows the pattern whereby we discover the files we need to work with and then we perform the required actions. It is a common approach to developing administration scripts because we can test
that we're discovering the correct files before we perform any actions on them.

 Listing 1 Compressing a file  

function compress-file { [CmdletBinding()]
param ( [parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)] [string]$computer=".", [string]$path="c:\test",
[int]$size = 2MB  #1

$drive = Split-Path -Path $path -Qualifier
$folder = $path.Remove(0,2).Replace("\","\\") + "\\"  #2

$query = "SELECT * FROM CIM_DATAFILE WHERE Drive='$drive' AND Path='$folder' AND Filesize >
Get-WmiObject -ComputerName $computer -Query $query |  #3 foreach {
$_.Compress()  #4
#1 Parameters
#2 Drive and path
#3 Runs WMI query
#4 Compresses a file

The function starts by setting out the parameters it will accept (#1). Computer has been a common parameter in many scripts. The path to the folder we want to test is presented as a string. The file size is presented as an integer. CIM_DataFile handles file sizes in bytes. PowerShell translates 2 MB as 2097152 bytes automatically. Split-Path is used on the path to give us the drive letter (including colon) by using the Qualifier parameter (#2). The path is then modified to work in WMI queries. The query is created and run through Get-WmiObject (#3). Our results are piped into Foreach-Object, where we use the Compress method on the file (#4). This script could be converted to encrypt the files by replacing Compress() with Encrypt(). Note that we can't do both to the same file!

I have picked a file size of 2MB as the default size above which I will compress. If you want to use larger sizes, you may run into the limits of the int32 data type. The maximum file size you can present as a parameter is
PS> [int]::MaxValue / 1gb

If you want to use a larger value as your test point, the data type will have to be changed to [int64]. This allows much larger file sizes to be used.
PS> [int64]::MaxValue /1gb

In case you are curious (I know I was), this translates to a large amount of storage.
PS> [int64]::MaxValue / 1pb

If you have files this big then I think we may need to look at other techniques.
Compressing files is a good thing when we are short on disk space. If, at a later date, you acquire more storage, you may want to uncompress these files so that the overhead of uncompressing the data to work on it and then compressing is removed.

WARNING Do not compress, or encrypt, SQL Server data files, Exchange databases or similar files with high I/O
levels. The overhead can have a severe impact on performance.

The function to perform the uncompression is shown in listing 2. I have called it expand-file to ensure I am using the recommended verb. It stops Import-Module throwing up warning messages about unapproved verbs.

 Listing 2 Uncompressing a file  

function expand-file { [CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]$computer=".", [string]$path="c:\test"

$drive = Split-Path -Path $path -Qualifier
$folder = $path.Remove(0,2).Replace("\","\\") + "\\"

$query = "SELECT * FROM CIM_DATAFILE WHERE Drive='$drive' AND Path='$folder' AND Compressed =
Get-WmiObject -ComputerName $computer -Query $query |
foreach {

If we compare it with listing 1, we will see that the file size parameter has been removed. Our query looks for compressed files in the folder and we use the Uncompress method.
The scripts have been deliberately restricted to a single folder for ease of use. They can be modified to work
against the whole drive by restricting the query to test just the drive.

Files are where we store our data. They may be valuable or they may be worthless a few hours after they are written. Users become frustrated when they can't find a file and administrators become frustrated when the users don't clean up their old files. We can use the file management capabilities of WMI to remove both of these frustrations.
Once we have found our files we usually want to do something with them. The usual suspects include copying, renaming, and deleting. We could also change the compression or encryption state of the files. We showed you how to compress and uncompress a file.

PowerShell and WMI


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend