4.14. Compressing a File or Folder

Problem

You want to regain some space on the hard disk by compressing files or folders.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. In the left pane, browse to the parent folder of the file or folder you want to compress. Click on the parent folder. This displays the list of subfolders and files in the right pane.

  3. In the right pane, right-click on the target file or folder and select Properties.

  4. Click the Advanced button.

  5. Check the box beside Compress contents to save disk space.

  6. Click OK and Apply.

Using a command-line interface

The compact command can compress and decompress files similar to Windows Explorer. The following command compresses all files in the current directory (/c option) and all subdirectories (/s option):

> compact /c /s

This command also causes all future files added anywhere under the current directory to be compressed.

To decompress all the files in the current directory and cause all future files to not be compressed, use the /u option with /s:

> compact /u /s

The following command compresses all of the files with the .doc extension (i.e., Word documents) in the c:\docs directory:

> compact /c /s:c:\docs *.doc

The compress.exe utility works a little bit differently from compact. It doesn't compress files transparently within the filesystem using NTFS compression. Instead it creates a compressed copy of a file. Here is an example:

> compress largetextfile.txt compressedfile.txt

The source file (largetextfile.txt) remains unchanged and the target file (compressedfile.txt) is a compressed version of that file. To compress all of the files in a directory, use this command:

> compress -R *.*

This creates compressed versions of each file and names them by replacing the last character of the source file with an underscore. For example, the compressed version of test.txt would be named test.tx_.

Tip

To decompress files that you compressed with the compress command, you must use the extract command.

Using VBScript

' This code compresses a 
folder and its contents using NTFS compression
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strFile     = "<FilePath>" ' e.g., d:\scripts\test.vbs
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objFile = objWMI.Get("Cim_Datafile='" & strFile & "'")
WScript.Echo objFile.Name
intRC = objFile.Compress ' To uncompress change this to objFile.Uncompress
if intRC <> 0 then
   WScript.Echo "There was an error compressing the file: " & intRC
else
   WScript.Echo "File compression successful"
end if
   
' This code compresses a folder and its contents using NTFS compression
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strFolder   = "<FolderPath>" ' e.g. d:\scripts
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objFolder = objWMI.Get("Win32_Directory='" & strFolder & "'")
intRC = objFolder.Compress  ' To uncompress change this to objFolder.Uncompress
if intRC <> 0 then
   WScript.Echo "There was an error compressing the folder: " & intRC
else
   WScript.Echo "Folder compression successful"
end if

Discussion

NTFS compression is a great feature because once you compress a file, NTFS handles decompressing it automatically for you when you attempt to view it or copy/move it to another folder. However, you shouldn't start using compression everywhere. Decompressing and compressing files is CPU intensive. Be very careful when enabling compression on frequently accessed or modified files because it can have an adverse impact on performance.

Warning

NTFS compression works only on partitions that were formatted using a 4-KB cluster size (the default) or smaller. See MS KB 171892 for more information.

See Also

MS KB 171892 (Err Msg: The File System Does Not Support Compression), MS KB 198038 (INFO: Useful Tools for Package and Deployment Issues), MS KB 251186 (Best Practices for NTFS Compression in Windows), MS KB 307987 (HOW TO: Use File Compression in Windows XP), MS KB 314958 (How To Use the COMPRESS, COMPACT, and EXPAND Commands to Compress and Expand Files and Folders in Windows 2000), and MS KB 323425 (HOW TO: Use the COMPACT Command to Compress and Uncompress Files and Folders in Windows Server 2003)

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.