【问题标题】:Find the lowest subfolders and zip them找到最低的子文件夹并压缩它们
【发布时间】:2011-08-04 13:38:58
【问题描述】:

Visual Studio 模板的文件夹结构如下:

/ProjectTemplatesCache
    /CSharp
        /Windows
            /1033
                /ClassLibrary.zip -- the lowest subfolder
                    /Properties
                /WindowsService.zip -- the lowest subfolder
            /1042
    /VisualBasic

我想从根文件夹开始,深入到最低的子文件夹,然后将它们分别压缩到一个单独的存档中。

使用 Windows Batch 或 C#。

如何压缩 - 没关系。只需能够分别选择每个/对每个执行命令。

有什么想法吗?

【问题讨论】:

  • “最低”是否意味着它不包含子文件夹?另外,是否有任何命名约定有助于获取文件夹层次结构?
  • @khachik:好问题!谢谢。我认为最低的是名为“*.zip”(仍然是一个文件夹)

标签: c# recursion batch-file visual-studio-templates


【解决方案1】:

C# 4.0:

var leafsDirs = Directory
    .EnumerateDirectories(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories)
    .Where(sub => !Directory.EnumerateDirectories(sub).Any());

leafsDirs.ToList().ForEach(Console.WriteLine);

查看ICSharpZip 进行实际压缩

更新

在@khachik 的建议之后,我的标准应该是 di.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)

好的,只是将条件与Where()混合在一起

只要模板文件夹可以包含一些子文件夹,例如ASP.NET MVC Web 应用程序

您始终可以提供Func<DirectoryInfo, bool> 来决定要压缩哪些目录

【讨论】:

    【解决方案2】:

    下面的代码递归地遍历目录树。您应该能够将其用作代码的基础:

    void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;
    
    
        // First, process all the files directly under this folder
        try
        {
            files = root.GetFiles("*.*");
        }
        // This is thrown if even one of the files requires permissions greater
        // than the application provides.
        catch (UnauthorizedAccessException e)
        {
        }
        catch (System.IO.DirectoryNotFoundException e)
        {
        }
    
        if (files != null)
        {
          foreach (System.IO.FileInfo fi in files)
          {
    
          }
    
          // Now find all the subdirectories under this directory.
          subDirs = root.GetDirectories();
    
    
          foreach (System.IO.DirectoryInfo dirInfo in subDirs)
          {
            // Resursive call for each subdirectory.
            WalkDirectoryTree(dirInfo);
          }
        }
    }
    

    如果没有更多目录可下载,您可以只返回一个布尔值,然后压缩所有文件。

    【讨论】:

      【解决方案3】:

      那么,您本质上是想查找所有没有子目录的子目录?

      var root = new DirectoryInfo(startPath);
      var lowestSubFolders =
          root.EnumerateDirectories("*", SearchOption.AllDirectories)
              .Where(di => di.EnumerateDirectories().Count() == 0);
      

      当然,如果存在当前用户无权访问的子目录等问题,但我觉得这段代码是为了一些在受控环境中运行的便利工具。

      【讨论】:

      • 志同道合...但是使用 !.Any() 胜过 .Count()==0 尤其是对于大/许多目录
      • 在@khachik 的建议之后,我的标准似乎是di.Name.EndsWith(".zip"),因为模板文件夹可以包含一些子文件夹,例如ASP.NET MVC Web 应用程序
      【解决方案4】:

      如果您使用的是 C#,那么:

      ...如果你真的想un压缩,那么试试Unzip files programmatically in .net

      【讨论】:

      • 谢谢。要存档我不能打电话给Process.Start("winrar.exe", args)。我的主要问题不是如何枚举子文件夹,而是如何找到最低的。
      • 您通过递归枚举找到最低的目录(“叶”目录),直到找到没有更多子目录的目录。
      猜你喜欢
      • 1970-01-01
      • 2014-10-01
      • 2019-03-23
      • 2019-05-07
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多