目录的大小

Size of a directory

本文关键字:      更新时间:2023-10-16

有没有一种方法可以在不实际遍历该目录并添加其中每个文件的大小的情况下获得目录大小/文件夹大小?理想情况下,希望使用一些类似boost的库,但win-api也可以。

据我所知,在大多数操作系统上,您必须通过迭代来做到这一点。

你可以看看boost.filesystem,这个库有一个递归_目录_迭代器,它会在系统上的任何文件累积大小的情况下进行迭代。

http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-递归目录迭代器

include <boost/filesystem.hpp>
int main()
{
    namespace bf=boost::filesystem;
    size_t size=0;
    for(bf::recursive_directory_iterator it("path");
        it!=bf::recursive_directory_iterator();
        ++it)
    {   
        if(!bf::is_directory(*it))
            size+=bf::file_size(*it);
    }   
}

PS:您可以通过使用std::accumulate和lambda I使其更加干净,只需CBA

我不认为有这样的东西,至少没有win32api函数。

窗口原生:

void DirectoryInfo::CalculateSize(std::string _path)
{
    WIN32_FIND_DATAA data;
    HANDLE sh = NULL;
    sh = FindFirstFileA((_path+"\*").c_str(), &data);
    if (sh == INVALID_HANDLE_VALUE )
    {
            return;
    }
    do
    {
        // skip current and parent
        if (std::string(data.cFileName).compare(".") != 0 && std::string(data.cFileName).compare("..") != 0)
        {
            // if found object is ...
            if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
            {
                // directory, then search it recursievly
                this->CalculateSize(_path+"\"+data.cFileName);

            } else
            {
                // otherwise get object size and add it to directory size
                this->dirSize += (__int64) (data.nFileSizeHigh * (MAXDWORD ) + data.nFileSizeLow);
            }
        }
    } while (FindNextFileA(sh, &data)); // do
    FindClose(sh);
} 

您必须遍历这些文件。如果树中存在硬链接或重分析点,则要获得正确的结果是很困难的。有关详细信息,请参阅陈的博客文章。

Zilog已经写出了相当好的答案,但我会用类似但不同的方式来做。

我有我的类型定义文件与:

typedef std::wstring String;
typedef std::vector<String> StringVector;
typedef unsigned long long uint64_t;

代码为:

uint64_t CalculateDirSize(const String &path, StringVector *errVect = NULL, uint64_t size = 0)
{
    WIN32_FIND_DATA data;
    HANDLE sh = NULL;
    sh = FindFirstFile((path + L"\*").c_str(), &data);
    if (sh == INVALID_HANDLE_VALUE )
    {
        //if we want, store all happened error  
        if (errVect != NULL)
            errVect ->push_back(path);
        return size;
    }
    do
    {
        // skip current and parent
        if (!IsBrowsePath(data.cFileName))
        {
            // if found object is ...
            if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
                // directory, then search it recursievly
                size = CalculateDirSize(path + L"\" + data.cFileName, NULL, size);
            else
                // otherwise get object size and add it to directory size
                size += (uint64_t) (data.nFileSizeHigh * (MAXDWORD ) + data.nFileSizeLow);
        }
    } while (FindNextFile(sh, &data)); // do
    FindClose(sh);
    return size;
} 
bool IsBrowsePath(const String& path)
{
    return (path == _T(".") || path == _T(".."));
}

这将使用UNICODE并返回失败的目录(如果需要的话)。

调用使用:

StringVector vect;
CalculateDirSize(L"C:\boost_1_52_0", &vect);
CalculateDirSize(L"C:\boost_1_52_0");

但决不能通过size

相关文章:
  • 没有找到相关文章