3.9. Compressing a Volume

Problem

You want to compress a volume.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. Right-click the drive you want to compress and select Properties.

  3. Click the General tab if it isn't opened by default.

  4. Check the box beside Compress drive to save disk space and click OK.

  5. Select the radio button beside the option for compressing only the files contained directly under C: or compressing all subfolders and files on the drive.

  6. Click OK.

Using a command-line interface

The following command causes files only at the root of drive D: to be compressed:

> compact /c d:\

Add the /s option to compress all files and folders on drive D:

> compact /c /s d:\

Using VBScript

' This code compresses a volume.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strDrive = "<Drive>"  ' e.g., D:
boolRecursive = True
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colFolder = objWMI.ExecQuery("select * from Win32_Directory " & _
                                 " where name = '" & strDrive & "\\'")
if colFolder.Count <> 1 then
   WScript.Echo "Error: Volume not found."
else
   for each objFolder in colFolder
      intRC = objFolder.CompressEx(strErrorFile,,boolRecursive)
      if intRC <> 0 then
         WScript.Echo "Error compressing volume: " & intRC 
         WScript.Echo "Stopped on file: " & strErrorFile
      else
         WScript.Echo "Successfully compressed volume."
      end if
   next 
end if

Discussion

Compressing an entire volume is a good idea if disk space utilization is a concern and you ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.