Compressing and Uncompressing Folders

Microsoft® Windows® 2000 Scripting Guide

Compression provides a way to free additional storage space on a disk drive without purchasing new hardware and without removing files or folders. Depending on the size of your hard disk and the type of files stored on that disk, you might be able to recover hundreds of megabytes of disk space and thus preclude the need to purchase a new hard drive and to take the computer offline until the new drive is installed.

The Win32_Directory class includes a Compress method that compresses all the files and subfolders within a specified folder. In addition, the class also includes an Uncompress method that removes compression from all the files and subfolders in a folder. Similar methods are also provided with the CIM_Datafile class. This allows you to selectively compress or uncompress specific files within a folder.

Because compression imparts a slight performance penalty, it is not recommended for files or folders that are accessed on a routine basis; for example, you probably do not want to compress database files, log files, or user profile folders. Better candidates for compression are files and folders that are not accessed very often. For example, you might write a script to return a collection of folders on a drive that have not been accessed for a month or more and then compress each of those folders.

The amount of disk space freed by compressing folders varies depending on the type of files stored in that folder. For example, .jpg files are already compressed, and further compression has little effect on the size of the file. With other file types, however, the savings can be considerable. For example, a new folder was created on a Windows 2000-based test computer, and 33 Microsoft Word documents, taking up a total of 15 megabytes (MB) of disk space, were copied into that folder. When the documents were compressed, the folder took up only 7 MB of disk space.

Scripting Steps

Listing 11.15 contains a script that compresses the folder C:\Scripts. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Directory class.

    To limit data retrieval to a specified folder, a Where clause is included restricting the returned folders to those with the name C:\\Scripts. You must include both backslashes (\\) in the name.

  4. For the single folder in the collection, use the Compress method to compress the folder, and then echo the results of that operation. The value 0 indicates that the folder was successfully compressed.

Listing 11.15 Compressing Folders

  
1
2
3
4
5
6
7
8
9
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE Name = 'c:\\Scripts'")
For Each objFolder in colFolders
 errResults = objFolder.Compress
 Wscript.Echo errResults
Next

To uncompress a folder, follow exactly the same procedure, but call the Uncompress method rather than the Compress method. For example, the script shown in Listing 11.16 uncompresses the folder C:\Scripts.

Listing 11.16 Uncompressing Folders

  
1
2
3
4
5
6
7
8
9
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE Name = 'c:\\Scripts'")
For Each objFolder in colFolders
 errResults = objFolder.Uncompress
 Wscript.Echo errResults
Next