Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all folders and subdirectories that doesn't have a file of certain extension

Tags:

c#

directory

gtk#

I'm using this solution to delete all empty folders and subdirectories in a certain path:

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);
        }
    }
}

It works perfectly. But I want to delete all empty folders and also folders which are not empty but also doesn't contain files with the .dvr extension.

For example, my folder has the files:

a.log

b.log

c.dvr

d.dat

So this folder can't be deleted, for it contains a file with the dvr extension.

How can I filter it? (I'm using GTK# but I believe C# code will work, since this solution is a C# code)

like image 961
Phiter Avatar asked Dec 14 '15 16:12

Phiter


People also ask

How do I delete a file without an extension?

Use Del *. in batch file to remove files with no extension. use Dir /A-D *. to list down all files with no extension.

How do I delete a file with a specific extension?

Using rm Command To remove a file with a particular extension, use the command 'rm'. This command is very easy to use, and its syntax is something like this. In the appropriate command, 'filename1', 'filename2', etc., refer to the names, plus their full paths.

Is there a way to erase all files in the current directory including all its subdirectories using only one command?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r .


2 Answers

Unfortunately error handling is very exception based in IO operations. And Directory.Delete throws an IOException if the directory is not empty. So you'll have to delete the files manually:

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;
}
like image 137
René Vogt Avatar answered Nov 14 '22 22:11

René Vogt


I would use Directory.EnumerateFiles to see if a directory contains the file you're looking for. Changing your code to be:

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);
        }
    }
}
like image 24
Avitus Avatar answered Nov 14 '22 21:11

Avitus