Recipe 8.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 in the file system 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 (compressed file.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_.

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


Using downloadable software

One of the most well-known third-party tools, which most Windows users have used at one point or another deals with compressing files. WinZip (http://winzip.com/) is a simple tool that lets you create compressed archives of files and folders.

You can download an evaluation version of WinZip that is good for 21 days. After 21 days, you must purchase a license for $29 to continue to use the tool.

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 for you automatically when you attempt to view it, copy it, or 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.

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.


Compressing files and folders with WinZip is a little different than using NTFS compression. If you want to send several large files to a colleague, your best option is to create a WinZip archive of the files and send the archive, not the individual files. This reduces the overall footprint of the files (assuming they are not already compressed).

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"



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net