#include <iostream>
#include <Windows.h>
#include <string.h>
#include <stdio.h>
#define LOCAL_MAX_PATH 2048
int TotalFiles;
int TotalDirectories;
void SearchDirectory(LPCTSTR lpPath)
{
TCHAR FindDir[LOCAL_MAX_PATH] = { 0 };
TCHAR SubDir[LOCAL_MAX_PATH] = { 0 };
TCHAR FullFileName[LOCAL_MAX_PATH] = { 0 };
WIN32_FIND_DATA FindFileData;
BOOL bRet = TRUE;
wcscpy(FindDir, lpPath);
wcscat(FindDir, L"\\*.*");
HANDLE hFind = FindFirstFile(FindDir, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind)
{
return;
}
char strFileName[LOCAL_MAX_PATH];
while (bRet == TRUE)
{
if (wcscmp(FindFileData.cFileName, L".") != 0
&& wcscmp(FindFileData.cFileName, L"..") != 0)
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TotalDirectories++;
wcscpy(SubDir, lpPath);
wcscat(SubDir, L"\\");
wcscat(SubDir, FindFileData.cFileName);
WideCharToMultiByte(CP_OEMCP, NULL, FindFileData.cFileName, -1, strFileName, 1024, NULL, FALSE);
//printf("%d---the dir name is %s\n", TotalDirectories, strFileName);
SearchDirectory(SubDir);
}
else
{
TotalFiles++;
wcscpy(FullFileName, lpPath);
wcscat(FullFileName, L"\\");
wcscat(FullFileName, FindFileData.cFileName);
WideCharToMultiByte(CP_OEMCP, NULL, FullFileName, -1, strFileName, 1024, NULL, FALSE);
//printf("%d---the file name is %s\n", TotalFiles, strFileName);
}
}
bRet = FindNextFile(hFind, &FindFileData);
}
FindClose(hFind);
}
void getDrivesFreeSpace(const TCHAR* strDriver)
{
unsigned long long available, total, free;
if (GetDiskFreeSpaceEx(strDriver, (ULARGE_INTEGER*)& available, (ULARGE_INTEGER*)& total, (ULARGE_INTEGER*)& free))
{
printf("Drives %s | total = %lld MB,available = %lld MB,free = %lld MB\n",
strDriver, total >> 20, available >> 20, free >> 20);
}
else
{
puts("获取容量信息失败");
}
}
void searchSingleDrive(const TCHAR* strDriver)
{
LARGE_INTEGER Freq, Start, End;
//TCHAR Path[MAX_PATH];
//auto strDriver = "D:";
//MultiByteToWideChar(CP_ACP, 0, strDriver, strlen(strDriver) + 1, Path, sizeof(Path));
QueryPerformanceFrequency(&Freq);
QueryPerformanceCounter(&Start);
SearchDirectory(strDriver);
QueryPerformanceCounter(&End);
printf("Total Files [%d] Total Directories [%d]\n", TotalFiles, TotalDirectories);
printf("Counter [%lld] Freq [%lld], Total [%lld] ms\n",
End.QuadPart - Start.QuadPart,
Freq.QuadPart,
(End.QuadPart - Start.QuadPart) * 1000 / Freq.QuadPart);
}
int main(int argc, char* argv[])
{
DWORD dwSize = MAX_PATH;
TCHAR szLogicalDrives[MAX_PATH] = { 0 };
UINT uDriverType = DRIVE_UNKNOWN;
//获取逻辑驱动器号字符串
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
//处理获取到的结果
if (dwResult > 0 && dwResult <= MAX_PATH) {
TCHAR* szSingleDrive = szLogicalDrives; //从缓冲区起始地址开始
while (*szSingleDrive)
{
uDriverType = GetDriveType(szSingleDrive);
switch (uDriverType) {
case DRIVE_UNKNOWN:
puts("未知的磁盘类型");
break;
case DRIVE_NO_ROOT_DIR:
puts("路径无效");
break;
case DRIVE_REMOVABLE:
puts("可移动磁盘");
break;
case DRIVE_FIXED:
printf("固定磁盘: %s\n", szSingleDrive);
getDrivesFreeSpace(szSingleDrive);
searchSingleDrive(szSingleDrive);
break;
case DRIVE_REMOTE:
printf("网络磁盘: %s\n", szSingleDrive);
break;
case DRIVE_CDROM:
puts("光驱");
break;
case DRIVE_RAMDISK:
puts("内存映射盘");
break;
default:
break;
}
// 获取下一个驱动器号起始地址
szSingleDrive += wcslen(szSingleDrive) + 1;
}
}
return 0;
}