遍历找出系统中的大文件

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值