-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrives.cpp
63 lines (54 loc) · 1.87 KB
/
drives.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
LPCTSTR GetDriveTypeStr(UINT dt)
{
switch (dt)
{
case DRIVE_UNKNOWN: return _T("Unknown");
case DRIVE_NO_ROOT_DIR: return _T("No Root Dir");
case DRIVE_REMOVABLE: return _T("Removable");
case DRIVE_FIXED: return _T("Fixed");
case DRIVE_REMOTE: return _T("Remote");
case DRIVE_CDROM: return _T("CdRom");
case DRIVE_RAMDISK: return _T("RamDisk");
default: return _T("Unknown");
}
}
static const TCHAR *HumanSize(ULONGLONG bytes, TCHAR* output, size_t sizeOfOutput)
{
TCHAR *suffix[] = { _T("B "), _T("KB"), _T("MB"), _T("GB"), _T("TB") };
int i = 0;
double dblBytes = double(bytes);
if (bytes > 1024)
{
for (i = 0; (bytes / 1024) > 0 && i < ARRAYSIZE(suffix)-1; ++i, bytes /= 1024)
dblBytes = bytes / 1024.0;
}
sprintf_s(output, sizeOfOutput, _T("%.02lf %s"), dblBytes, suffix[i]);
return output;
}
int _tmain(/*int argc, const TCHAR* const argv[]*/)
{
TCHAR drives[1024];
GetLogicalDriveStrings(ARRAYSIZE(drives), drives);
for (const TCHAR* d = drives; *d != _T('\0'); d += _tcslen(d) + 1)
{
TCHAR VolumeLabel[100];
GetVolumeInformation(d, VolumeLabel, ARRAYSIZE(VolumeLabel), nullptr, nullptr, nullptr, nullptr, 0);
ULARGE_INTEGER Quota, Total, Free;
GetDiskFreeSpaceEx(d, &Quota, &Total, &Free);
double usage = Free.QuadPart * 100.0 / Total.QuadPart;
TCHAR TotalStr[15];
TCHAR FreeStr[15];
_tprintf(_T("%-3s %-10s %-10s %10s %10s %6.2lf%% Free\n"),
d,
VolumeLabel,
GetDriveTypeStr(GetDriveType(d)),
HumanSize(Total.QuadPart, TotalStr, ARRAYSIZE(TotalStr)),
HumanSize(Free.QuadPart, FreeStr, ARRAYSIZE(FreeStr)),
usage);
}
return EXIT_SUCCESS;
}