简体   繁体   English

删除指定路径中的所有空目录

[英]Delete all empty directories in specified path

In my program, I create a long branching tree of directories. 在我的程序中,我创建了一个长长的目录树。 When I'm done with certain files on the leaves of this tree, I delete them, but I end up with a lot of empty parent directories. 当我在这棵树的叶子上处理完某些文件后,便将其删除,但最终会出现许多空的父目录。 I'd like to delete these as well. 我也想删除这些。 However, I can't just recursively delete all these parent directories, because some of them have children that I can't delete yet. 但是,我不能只递归地删除所有这些父目录,因为其中一些父目录尚无法删除。

Example: C:\\MyProject\\Project1\\file1\\file2\\file3\\file4\\file5\\document.txt 示例:C:\\ MyProject \\ Project1 \\ file1 \\ file2 \\ file3 \\ file4 \\ file5 \\ document.txt

If I delete document.txt, I want to also delete all the other empty folders in the path. 如果删除document.txt,我还要删除路径中的所有其他空文件夹。 However, file 3 has something in it (besides file4), so I can't delete it or anything above it. 但是,文件3中有文件(文件4除外),因此我无法删除它或文件之上的任何内容。 So in this case, file4 and file5 would be deleted. 因此,在这种情况下,文件4和文件5将被删除。

Consider the root directory to be Project1. 将根目录视为Project1。 I don't want to delete anything above that. 我不想删除以上任何内容。

Has anyone written something to do this? 有没有人写一些东西做这个?

Basically, something I can call where I can specify the path I'm trying to remove from the tree (first arg below), along with the root of the tree (second arg). 基本上,我可以在这里指定可以从树中删除的路径(下面的第一个arg)以及树的根(第二个arg)。

DeleteEmptySubDirectoriesInPath("C:\MyProject\Project1\file1\file2\file3\file4\file5\",
"C:\MyProject\Project1");

Another way to look at it is the inverse of Directory.CreateDirectory. 另一种查看方式是Directory.CreateDirectory的逆函数。 This is the function I used to generate these long branches. 这是我用来生成这些长分支的函数。 Now I need to remove them when I'm done without disturbing anything else. 现在,我需要在完成操作后删除它们,而不会打扰其他任何东西。

A little bit of iteration would work: 进行一点迭代即可:

var di = new DirectoryInfo(@"C:\MyProject\Project1\file1\file2\file3\file4\");
var root = @"C:\MyProject\Project1"; // no trailing slash!
while (di.FullName != root 
       && !di.EnumerateFiles().Any() 
       && !di.EnumerateDirectories().Any())
{
    di.Delete();
    di = di.Parent;
}

Start from the directory of interest; 从感兴趣的目录开始; as long as there are no files and you have not reached the "root", delete it and move to its parent directory. 只要没有文件并且您还没有到达“ root”,请将其删除并移至其父目录。 Repeat until done. 重复直到完成。

Doesn't seem like it should be any more difficult than this: 似乎没有比这更困难的了:

  • We delete the file 我们删除文件
  • Walk up the directory tree, deleting [empty] directories as we go until we get to the root. 遍历目录树,逐步删除[空]目录,直到到达根目录为止。

Here's the code: 这是代码:

static void DeleteAndPrune(string path)
{
  FileInfo fi = new FileInfo(path);
  fi.Delete();

  // the loop ends with the first non-empty directory, or the root.
  // - if the directoryInfo itself is null, the file was deleted *from* the root,
  //   so there's nothing to do.
  // - if the directoryInfo's parent is null, we've hit the root directory
  // Easy!      
  for ( DirectoryInfo di = fi.Directory ; di != null && di.Parent != null && !di.EnumerateFileSystemInfos().Any() ; di = di.Parent )
  {
    di.Delete() ;
  }

  return;
}

It's almost simpler if you want to just walk the entire tree and tidy it to remove empty directories. 如果您只想遍历整棵树并对其进行整理以删除空目录,则几乎简单得多。 Just a simple recursive tree walk: 只是一个简单的递归树遍历:

    static void Tidy( DirectoryInfo tree )
    {
        foreach (DirectoryInfo di in tree.EnumerateDirectories())
        {
            Tidy(di);
        }
        tree.Refresh();
        if (!tree.EnumerateFileSystemInfos().Any())
        {
            tree.Delete();
        }
        return;
    }

That can be done like so: 可以这样做:

DirectoryInfo root = new DirectoryInfo( @"C:\MyProject\Project1" ) ;
Tidy(root) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM