Programming Field

Compact - DOS/Command Prompt Reference

Compresses or decompresses files on NTFS.

Syntax

compact[.exe] [/C | /U] [/S[:<directory>]] [/A] [/I] [/F] [/Q]
    [/EXE[:<algorithm>]] [/CompactOs[:<option>] [/WinDir:<directory>]]
	[<file-name> [...]]

Options

If no options are specified, it outputs the compression status of the current directory and the files within it.

/C | /U Specifying /C compresses the files, while /U uncompresses them. To change the compression status, use /C or /U accordingly.
/S[:<directory>]

Targets not only the files directly under the specified directory but also all subdirectories and their files. If you specify a colon ‘:’ and a directory name (directory path), Compact targets that specific directory. If not specified, it targets the current directory.

If neither /C nor /U is specified, it displays the compression status of all subdirectories and files. If /C or /U is specified, it changes the compression status of all subdirectories and files accordingly.

/A

Displays files with attributes of hidden and system files. By default, these files are not shown.

* Even though they are not displayed, if they are included as targets, they will still be affected by changes in compression status using /C and /U.

/I Continues processing even if an error occurs during the operation. If /I is not specified, the process will be aborted when an error occurs, and subsequent files will not be processed.
/F Forces compression or decompression even if the compression status does not change. By default, when /C is specified, it skips already compressed files, and when /U is specified, it skips uncompressed files. This option is useful when you want to change the compression algorithm using /EXE.
/Q Simplifies the output during processing.
/EXE[:<algorithm>]

[Windows 10 or later] Uses a compression algorithm tailored to the characteristics of executable files (frequently read and infrequently modified). <algorithm> will be the algorithm (following a colon ‘:’), using one of the following algorithms (case-insensitive).

NameNotes
XPRESS4KThe default value when only "/EXE" is specified; fastest read
XPRESS8K
XPRESS16K
LZXMaximum compression

If you specify only ‘/EXE’ without providing <algorithm>, it defaults to ‘XPRESS4K’. If you compress without specifying /EXE, the default algorithm (not available with /EXE) will be used.

/CompactOs[:<option>

[Windows 10 or later] Displays or sets the compression status of system files. <option> (after a colon ‘:’) will be one of the following values (case-insensitive).

NameEffect
queryDisplays the current status and exits.
alwaysEnables compression and compresses system binaries.
neverDisables compression and decompresses system binaries.

If only ‘/CompactOs’ is specified, it displays the current status, similar to ‘/CompactOs:query’. Note that administrative privileges are required for state changes.

/WinDir:<directory> [Windows 10 or later] Displays the compression status of system files on a currently not running Windows. <directory> will be the Windows directory of the inactive Windows. This option must always be used with ‘/CompactOs:query’. Administrative privileges are required for the inquiry.
<file-name> [...]

Specifies individual file names for displaying or changing compression status. You can specify multiple files (in the place of the [...] brackets).

If /S is specified, it will be treated as a relative path from the path specified with /S. However, if <file-name> does not contain a pathname (only the filename), it targets all files with the same filename in all directories and subdirectories.

Details

NTFS file system has a feature to compress and store files. By using Compact, you can manage the compression and non-compression status of files. Even if a file is compressed, it can be handled like a regular file (although there may be potential performance drawbacks in terms of read and write compared to uncompressed state).

Compressed files do not appear in the attributes of Attrib, but they do appear in the attributes output by ‘%~a1’.

* This compression feature is only available on NTFS and cannot be used on ReFS.

Samples

Sample 1

compact

Displays the compression status of the current directory and the files within it on the screen.

Sample 2

compact /S:D:\document /C

Compresses the ‘D:\document’ directory and all its subdirectories and files.