Howto determine if a drive is compressed or not in Windows XP or later

For NTFS file-based compression method you will need to check the attributes of any specific file to see if it’s compressed. Usually it is enough to look at the attributes of C:\ but this is not
really needed to be compressed to have other files being compressed on the drive as NTFS supports to have just single files being compressed.

Here a function C# implementation to check the attribute:

// call this function like e.g.  bool b = IsCompressed(@”C:\”);
// when C:\ marked to be compressed, it will return true … says nothing about the whole drive!!!!
public static bool IsCompressed(string path)
{
return IsCompressed((FileSystemInfo)new FileInfo(path));
}

public static bool IsCompressed(FileSystemInfo fsi)
{
return (fsi.Attributes & FileAttributes.Compressed) == FileAttributes.Compressed;
}

For drive-based  compression method, like good old doublespace, you just need to get yourself the instance of Win32_LogicalDisk of the drive you want to check and look at the property “Compressed”.

e.g. for root\cimv2\Win32_LogicalDisk.DeviceID=”C:” you will get something like following:

instance of Win32_LogicalDisk
{
Caption = “C:”;
Compressed = FALSE;
CreationClassName = “Win32_LogicalDisk”;
Description = “Lokale Festplatte”;
DeviceID = “C:”;
DriveType = 3;
FileSystem = “NTFS”;
FreeSpace = “25166004224”;
MaximumComponentLength = 255;
MediaType = 12;
Name = “C:”;
QuotasDisabled = TRUE;
QuotasIncomplete = FALSE;
QuotasRebuilding = FALSE;
Size = “52427898880”;
SupportsDiskQuotas = TRUE;
SupportsFileBasedCompression = TRUE;
SystemCreationClassName = “Win32_ComputerSystem”;
SystemName = “XXXXXXX”;
VolumeDirty = FALSE;
VolumeName = “System”;
VolumeSerialNumber = “XXXXXXX”;
};

Here is a small VBScript which does this for the drive “C” …

Set oWmi = GetObject(“winmgmts:/root/cimv2”)
Set driveC = oWmi.Get(“Win32_LogicalDisk.DeviceID=””C:”””)

if not driveC.Properties_.Item(“Compressed”).Value then
WScript.echo(“Drive C: is NOT compressed.”)
else
WScript.echo(“Drive C: IS compressed.”)
end if

Ciao Thilo

This entry was posted in Windows. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *