#include <stdint.h>
#include <list>
#include <windows.h>
using namespace std;
#define MAX_PATH_LEN (8192)
#define G_UNIT (1024*1024*1024)
#define M_UNIT (1024*1024)
#define K_UNIT (1024)
char* BytesExchange(uint64_t bytes)
{
static char str[256] = { 0 };
if (bytes >= G_UNIT)
{
sprintf_s(str, sizeof(str), "%.2fG", bytes*1.0 / G_UNIT);
}
else if (bytes >= M_UNIT)
{
sprintf_s(str, sizeof(str), "%.2fM", bytes*1.0 / M_UNIT);
}
else if (bytes >= K_UNIT)
{
sprintf_s(str, sizeof(str), "%.2fK", bytes*1.0 / K_UNIT);
}
else
{
sprintf_s(str, sizeof(str), "%dbytes", bytes);
}
return str;
}
uint64_t TraverFolder(const char* dirname)
{
uint64_t TotalSize = 0;
list<string> dirlist;
dirlist.push_back(dirname);
while (!dirlist.empty())
{
string dirstr = dirlist.front();
dirlist.pop_front();
char findName[MAX_PATH_LEN] = { 0 };
strcpy(findName, dirstr.c_str());
strcat(findName, "\\*");
uint64_t DirSize = 0;
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA(findName, &wfd);
while (hFind && INVALID_HANDLE_VALUE != hFind)
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(wfd.cFileName, ".") != 0 &&
strcmp(wfd.cFileName, "..") != 0)
{
char FolderName[MAX_PATH_LEN] = { 0 };
sprintf_s(FolderName, sizeof(FolderName), "%s\\%s", dirstr.c_str(), wfd.cFileName);
dirlist.push_back(std::string(FolderName));
}
}
else
{
uint64_t FileSize = (uint64_t)wfd.nFileSizeHigh << 32 | (uint32_t)wfd.nFileSizeLow;
DirSize += FileSize;
}
if (!FindNextFileA(hFind, &wfd))
{
break;
}
}
//printf(" %s(%d) (%s)\n", dirstr.c_str(), dirstack.size(), BytesExchange(DirSize));
TotalSize += DirSize;
}
return TotalSize;
}
int main(int argc, char* argv[])
{
char dirName[] = "C:\\Windows";
char findName[MAX_PATH_LEN] = { 0 };
strcpy(findName, dirName);
strcat(findName, "\\*");
uint64_t DirSize = 0;
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA(findName, &wfd);
while (hFind && hFind != INVALID_HANDLE_VALUE)
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(wfd.cFileName, ".") != 0 &&
strcmp(wfd.cFileName, "..") != 0)
{
char FolderName[MAX_PATH_LEN] = { 0 };
sprintf_s(FolderName, sizeof(FolderName), "%s\\%s", dirName, wfd.cFileName);
uint64_t TotalSize = TraverFolder(FolderName);
printf("%s (%s)\n", FolderName, BytesExchange(TotalSize));
}
}
if (!FindNextFileA(hFind, &wfd))
{
break;
}
}
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交