Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to empty a directory?

Is there a way to delete all files & sub-directories of a specified directory without iterating over them?

The non elegant solution:

public static void EmptyDirectory(string path)
{
    if (Directory.Exists(path))
    {
        // Delete all files
        foreach (var file in Directory.GetFiles(path))
        {
            File.Delete(file);
        }

        // Delete all folders
        foreach (var directory in Directory.GetDirectories(path))
        {
            Directory.Delete(directory, true);
        }
    }
}
like image 655
HuBeZa Avatar asked Jul 26 '09 12:07

HuBeZa


People also ask

How do you empty a directory?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I empty a directory in Linux?

To permanently remove a directory in Linux, use either rmdir or rm command: For empty directories, use rmdir [dirname] or rm -d [dirname] For non-empty directories, use rm -r [dirname]

Which command is used for removing and empty directory?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only . and ..) before you can remove it, and you must have write permission in its parent directory.

How do I delete a folder with millions of files?

Navigate to the folder that you want to delete (with all its files and subfolders). Use cd *path*, for example, cd C:\Trash\Files\ to do so. Use cd .. to navigate to the parent folder and run the command RMDIR /Q/S *foldername* to delete the folder and all of its subfolders. This person is a verified professional.


3 Answers

How about System.IO.Directory.Delete? It has a recursion option, you're even using it. Reviewing your code it looks like you're trying to do something slightly different -- empty the directory without deleting it, right? Well, you could delete it and re-create it :)


In any case, you (or some method you use) must iterate over all of the files and subdirectories. However, you can iterate over both files and directories at the same time, using GetFileSystemInfos:

foreach(System.IO.FileSystemInfo fsi in 
    new System.IO.DirectoryInfo(path).GetFileSystemInfos())
{
    if (fsi is System.IO.DirectoryInfo)
        ((System.IO.DirectoryInfo)fsi).Delete(true);
    else
        fsi.Delete();
}
like image 181
BlueMonkMN Avatar answered Oct 13 '22 06:10

BlueMonkMN


Why is that not elegant? It's clean, very readable and does the job.

like image 41
Tommy Carlier Avatar answered Oct 13 '22 06:10

Tommy Carlier


Well, you could always just use Directory.Delete....

http://msdn.microsoft.com/en-us/library/aa328748%28VS.71%29.aspx

Or if you want to get fancy, use WMI to delete the directory.

like image 35
TheSmurf Avatar answered Oct 13 '22 07:10

TheSmurf