删除没有特定扩展名文件的所有文件夹和子目录

时间:2015-12-14 16:39:05

标签: c# directory gtk#

我正在使用此解决方案删除特定路径中的所有空文件夹和子目录:

static void Main(string[] args)
{
    processDirectory(@"c:\temp");
}

private static void processDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        processDirectory(directory);
        if (Directory.GetFiles(directory).Length == 0 && 
            Directory.GetDirectories(directory).Length == 0)
        {
            Directory.Delete(directory, false);
        }
    }
}

完美无缺。但我想删除所有空文件夹以及非空文件夹,但也不包含.dvr扩展名的文件。

例如,我的文件夹包含以下文件:

  

a.log

     

b.log

     

c.dvr

     

d.dat

因此,无法删除此文件夹,因为它包含带有dvr扩展名的文件。

我该如何过滤它? (我正在使用GTK#但我相信C#代码可以工作,因为这个解决方案是C#代码)

3 个答案:

答案 0 :(得分:5)

不幸的是,错误处理在IO操作中是非常例外的。如果目录不为空,则Directory.Delete会抛出IOException。因此,您必须手动删除文件:

private static bool processDirectory(string startLocation)
{
    bool result = true;
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        bool directoryResult = processDirectory(directory);
        result &= directoryResult;

        if (Directory.GetFiles(directory, "*.dvr").Any())
        {
             result = false;
             continue;
        }

        foreach(var file in Directory.GetFiles(directory))
        {
            try
            {
               File.Delete(file);
            }
            catch(IOException)
            {
               // error handling
               result = directoryResult = false;
            }
        }

        if (!directoryResult) continue;
        try
        {
            Directory.Delete(directory, false);
        }
        catch(IOException)
        {
            // error handling
            result = false;
        }
    }

    return result;
}

答案 1 :(得分:2)

我会使用Directory.EnumerateFiles查看目录是否包含您要查找的文件。将您的代码更改为:

static void Main(string[] args)
{
    processDirectory(@"c:\temp");
}

private static void processDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        processDirectory(directory);
        if (Directory.GetDirectories(directory).Length == 0  ||
            Directory.EnumerateFiles(directory, "*.dvr").Length == 0
            )
        {
            Directory.Delete(directory, false);
        }
    }
}

答案 2 :(得分:0)

Avitus answer是正确的,但由于您需要它在.NET 2.0中工作,因此无法使用EnumerateFiles方法,但GetFiles可以很好地完成工作。但需要更多代码。

示例:

static void Main(string[] args)
{
    processDirectory(@"c:\temp");
}

private static void processDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        processDirectory(directory);

        if (Directory.GetDirectories(directory).Length == 0))
        {
            bool delete = false;

            var files = Directory.GetFiles(directory);
            // This will delete the directory if it contains a file with the Extension
            // of .dvr, regardless if there are other files in there. Might be something you want to change.
            for (int i = 0; i < files.Length && !delete; i++)
            {
                delete = files[i].Extension.Equals(".dvr", StringComparison.OrdinalIgnoreCase);
            }

            if (delete)
            {
                // Recursive must be set to true in order to
                // delete files and sub-directories in the folder.
                // This folder will not have any sub-directories
                // so it's only used to delete the files.
                Directory.Delete(directory, /*recursive*/ true);
            }
        }
    }
}

如果你愿意的话,你可以根据较少代码的精神在Directory.Delete循环中移动for方法调用,然后打破循环。