#include <Windows.h>
#include <iostream>
#include <queue>
using namespace std;
char* wideCharToMultiByte(const wchar_t* pWCStrKey)
{
//第一次调用确认转换后单字节字符串的长度,用于开辟空间
int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey) & 0xffffffff, NULL, 0, NULL, NULL);
char* pCStrKey = new char[pSize + 1];
//第二次调用将双字节字符串转换成单字节字符串
WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey) & 0xffffffff, pCStrKey, pSize, NULL, NULL);
pCStrKey[pSize] = '\0';
return pCStrKey;
}
int QueryFileCounts(LPCTSTR Path, DWORD FileSize)
{
queue<std::wstring> qFolders;
qFolders.push(Path);
int fileCounts = 0;
WIN32_FIND_DATA findResult;
HANDLE handle = NULL;
while (qFolders.size() > 0)
{
std::wstring tempFolder = qFolders.front();
std::wstring CurrentFolder = tempFolder;
tempFolder.append(L"\\*.*");
handle = FindFirstFile(tempFolder.c_str(), &findResult);
do
{
if (findResult.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (lstrcmp(L".", findResult.cFileName) == 0 || lstrcmp(L"..", findResult.cFileName) == 0)
{
continue;
}
tempFolder = qFolders.front();
tempFolder.append(L"\\").append(findResult.cFileName);
qFolders.push(tempFolder);
}
else
{
if (findResult.nFileSizeLow > FileSize*1024*1024)
{
char* FileName = wideCharToMultiByte((CurrentFolder + findResult.cFileName).c_str());
string str = FileName;
delete[]FileName;
printf("大文件名:%s\n", str.c_str());
}
fileCounts++;
}
} while (FindNextFile(handle, &findResult));
qFolders.pop();
}
if (handle)
{
FindClose(handle);
handle = NULL;
}
return fileCounts;
}
int main(int argc, CHAR* argv[])
{
DWORD FileSize = 0;
printf("指定大文件尺寸:(M)\n");
scanf_s("%d", &FileSize);
DWORD dwSize = MAX_PATH;
WCHAR szLogicalDrives[MAX_PATH] = { 0 };
//获取逻辑驱动器号字符串
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
//处理获取到的结果
if (dwResult > 0 && dwResult <= MAX_PATH) {
WCHAR* szSingleDrive = szLogicalDrives; //从缓冲区起始地址开始
while (*szSingleDrive)
{
cout << "文件个数:" << QueryFileCounts(szSingleDrive, FileSize) << endl;
szSingleDrive += wcslen(szSingleDrive) + 1;
}
}
system("pause");
return 0;
}
遍历找出系统中的大文件
最新推荐文章于 2022-12-14 13:50:40 发布