3
\$\begingroup\$

Tell me how to speed up the removal of empty folders / files ?

string[] dirs = Directory.EnumerateDirectories(str1, "*", SearchOption.AllDirectories).ToArray();
DirectoryInfo dirInfo = new DirectoryInfo(str1);
for (int i = dirs.Length - 1; i >= 0; i--)
{
 if (Directory.EnumerateFileSystemEntries(dirs[i]).Count() == 0)
 Directory.Delete(dirs[i], true);
}
foreach(string pathToFile in Directory.GetFiles(str1))
{
 if (new FileInfo(pathToFile).Length == 0)
 {
 try
 {
 File.Delete(pathToFile);
 }
 catch(Exception ex)
 {
 // error
 }
 } 
}

Deletes files not from all folders.

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Apr 12, 2017 at 9:35
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Remark: A directory may become empty after deleting all empty files / directories in it. I think you missed this case. \$\endgroup\$ Commented Apr 12, 2017 at 9:39
  • 1
    \$\begingroup\$ Why using c#? If you use linux you can use command line commands see unix.stackexchange.com/questions/46322/… stackoverflow.com/questions/3157343/… \$\endgroup\$ Commented Apr 12, 2017 at 9:41

1 Answer 1

4
\$\begingroup\$
Directory.EnumerateDirectories(str1, "*", SearchOption.AllDirectories).ToArray();

Calling ToArray on this is counterproductive because this method returns directory names one by one (in a deferred fasion) as you go and request them. By using ToArray you go over directory names twice. First creating the array and then in your for loop. The EnumerateDirectories is a perfect candidate to be used with Parallel.ForEach or with AsParallel.

For example this is on my machine with 20k empty directories faster by approx 3 sec then the normal loop:

var emptyDirectories = Directory
 .EnumerateDirectories(str1, "*", SearchOption.AllDirectories)
 .AsParallel()
 .Where(d => !Directory.EnumerateFileSystemEntries(d).Any())
 .ToList();

Here you not only take the advantage of the AsParallel but also of the Any that does not have to enumerate all entries but stops as soon as at least one is found.

To delete the empty directories you can use the ForAll extension o a parallel enumeration:

var emptyDirectories =
 from d in Directory.EnumerateDirectories(str1, "*", SearchOption.AllDirectories).AsParallel()
 where !Directory.EnumerateFileSystemEntries(d).Any()
 select d;
emptyDirectories.ForAll(d => { /* delete directory */ });

The above method will of course delete only the last directory. If you want to delete directories that are empty after deleting the last empty directory you need to repeat the process:

var deleted = false;
do
{
 deleted = false;
 emptyDirectories.ForAll(d => { Directory.Delete(d); deleted = true; });
}
while(deleted);

The counterpart of the EnumerateDirectories is the EnumerateFiles method that you can use same way and you can figure out this part yourself ;-)

answered Apr 12, 2017 at 17:28
\$\endgroup\$
9
  • 1
    \$\begingroup\$ @ArteS It depends on what you are going to do. If you want to add cancellation then you might want to use Parallel.ForEach or Task but if you are as lazy as I am, you chose the easiest solution ;-) \$\endgroup\$ Commented Apr 12, 2017 at 18:07
  • \$\begingroup\$ @ArteS the same way you did it before .ForAll(d => Directory.Delete(d, true)); \$\endgroup\$ Commented Apr 12, 2017 at 18:31
  • \$\begingroup\$ @ArteS this is strange because it did work for me - I tested it before posting. \$\endgroup\$ Commented Apr 12, 2017 at 18:38
  • \$\begingroup\$ Updated the post \$\endgroup\$ Commented Apr 12, 2017 at 18:40
  • \$\begingroup\$ Sorry, earned. And how to clear empty files? \$\endgroup\$ Commented Apr 12, 2017 at 18:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.