首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何找到位于文件夹内的所有文件的大小?

如何找到位于文件夹内的所有文件的大小?
EN

Stack Overflow用户
提问于 2013-03-19 17:32:45
回答 12查看 19.8K关注 0票数 15

c++中有没有获取指定文件夹大小的接口?

如果没有,我怎样才能得到一个文件夹的总大小,包括所有子文件夹和文件?

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2013-03-19 22:06:08

实际上我不想使用任何第三方库。我只想用纯c++实现。

如果你使用MSVC++,你有 "as standard C++".,但是使用boost或MSVC -两者都是“纯C++”。

如果您不想使用boost,并且只想使用C++ std::库,那么这个答案有点接近。如您所见,。你可以在这里读到:

是该库的Boost版本,已经广泛使用了十年。该库的Dinkumware版本基于N1975 (相当于Boost库的版本2),随Microsoft Visual C++ 2012一起提供。

为了说明它的用法,我采用了@Nayana Adassuriya的答案,只做了很小的修改(好的,他忘记初始化一个变量,我使用unsigned long long,最重要的是使用:path filePath(complete (dirIte->path(), folderPath));在调用其他函数之前恢复完整路径)。我已经测试过了,它在windows7上运行得很好。

代码语言:javascript
运行
复制
#include <iostream>
#include <string>
#include <filesystem>
using namespace std;
using namespace std::tr2::sys;

void  getFoldersize(string rootFolder,unsigned long long & f_size)
{
   path folderPath(rootFolder);                      
   if (exists(folderPath))
   {
        directory_iterator end_itr;
        for (directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte )
        {
            path filePath(complete (dirIte->path(), folderPath));
           try{
                  if (!is_directory(dirIte->status()) )
                  {
                      f_size = f_size + file_size(filePath);                      
                  }else
                  {
                      getFoldersize(filePath,f_size);
                  }
              }catch(exception& e){  cout << e.what() << endl; }
         }
      }
    }

int main()
{
    unsigned long long  f_size=0;
    getFoldersize("C:\\Silvio",f_size);
    cout << f_size << endl;
    system("pause");
    return 0;
}
票数 9
EN

Stack Overflow用户

发布于 2013-03-19 19:14:30

让操作系统为你做这件事怎么样:

代码语言:javascript
运行
复制
long long int getFolderSize(string path) 
{
    // command to be executed
    std::string cmd("du -sb ");
    cmd.append(path);
    cmd.append(" | cut -f1 2>&1");

    // execute above command and get the output
    FILE *stream = popen(cmd.c_str(), "r");
    if (stream) {
        const int max_size = 256;
        char readbuf[max_size];
        if (fgets(readbuf, max_size, stream) != NULL) {
            return atoll(readbuf);
        }   
        pclose(stream);            
    }           
    // return error val
    return -1;
}
票数 10
EN

Stack Overflow用户

发布于 2013-03-19 18:36:43

你可以这样使用boost。您可以尝试对其进行更深层次的优化。

代码语言:javascript
运行
复制
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>



    using namespace std;
    namespace bsfs = boost::filesystem; 

    void  getFoldersize(string rootFolder,long & file_size){
        boost::replace_all(rootFolder, "\\\\", "\\");   
        bsfs::path folderPath(rootFolder);                      
        if (bsfs::exists(folderPath)){
            bsfs::directory_iterator end_itr;

            for (bsfs::directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte )
            {
                bsfs::path filePath(dirIte->path());
                try{
                    if (!bsfs::is_directory(dirIte->status()) )
                    {

                        file_size = file_size + bsfs::file_size(filePath);                      
                    }else{
                        getFoldersize(filePath.string(),file_size);
                    }
                }catch(exception& e){               
                    cout << e.what() << endl;
                }
            }
        }

    }

    int main(){
        long file_size =0;
        getFoldersize("C:\\logs",file_size);
        cout << file_size << endl;
        system("pause");
        return 0;
    }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15495756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档