查找目录的大小

时间:2011-10-06 08:40:54

标签: algorithm file recursion directory

我在思科采访中得到了这个问题:编写一个函数来查找目录的大小?

以下是这种函数的伪代码,它遵循递归方法。请告诉我是否还有其他方法。

int directorySize(DirectoryHandle dh)
{
    int size=0;
    if (!dh)
    {
        DirectoryHandle dh1 = directoryOpen("Directory_path");
    }
    else
    {
        dh1 = dh;
    }

    while (dh1)
    {
        if (TRUE=IsDirectory(dh1))
        {
            size += directorysize(dh1);
        }
        else if (TRUE == IsFile(dh1))
        {
            FileHandle fh = dh1;
            while (EOF != fh)
            {
                size++;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用nftw的典型示例:

请注意,随着面试问题的出现,他们可能会希望看到你在考虑

  • 遍历顺序
  • 权限(无法访问的子文件夹等)
  • 尺寸ondisk与外观尺寸
  • 符号链接,硬链接(在树外?重复计数?)
  • 稀疏文件
  • 性能

以下代码以实用的方式解决了大部分问题:

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

static uintmax_t total        = 0ul;
static uintmax_t files        = 0ul;
static uintmax_t directories  = 0ul;
static uintmax_t symlinks     = 0ul;
static uintmax_t inaccessible = 0ul;
static uintmax_t blocks512    = 0ul;

static int
display_info(const char *fpath, const struct stat *sb,
             int tflag, struct FTW *ftwbuf)
{
    switch(tflag)
    {
        case FTW_D:
        case FTW_DP:  directories++;  break;
        case FTW_NS:
        case FTW_SL:
        case FTW_SLN: symlinks++;     break;
        case FTW_DNR: inaccessible++; break;
        case FTW_F:   files++;        break;
    }
    total += sb->st_size;
    blocks512 += sb->st_blocks;
    return 0; /* To tell nftw() to continue */
}

int
main(int argc, char *argv[])
{
    int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;

    if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
    {
        perror("nftw");
        exit(EXIT_FAILURE);
    }

    printf("Total size: %7jd\n", total);
    printf("In %jd files and %jd directories (%jd symlinks and %jd inaccessible directories)\n", files, directories, symlinks, inaccessible);
    printf("Size on disk %jd * 512b = %jd\n", blocks512, blocks512<<9);

    exit(EXIT_SUCCESS);
}

之前发布为Fastest ways to get a directory Size and Size on disk。典型输出:

Total size: 28433001733
In 878794 files and 87047 directories (73318 symlinks and 0 inaccessible directories)
Size on disk 59942192 * 512b = 30690402304

答案 1 :(得分:1)

可能为大型文件集和更好的子目录导航添加更多空间。

long DirectoryLength(DirectoryInfo dir)
{
    long size = 0;
    foreach (FileInfo file in dir.GetFiles())
        size += file.Length;

    foreach (DirectoryInfo sub in dir.GetDirectories())
        size += DirectoryLength(sub);

    return size;
}