找到最低的子文件夹并压缩它们

发布于 2024-10-31 19:55:56 字数 448 浏览 0 评论 0原文

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

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

我想从根文件夹开始,深入到最低的子文件夹并将每个子文件夹压缩到一个单独的存档中。

使用 Windows 批处理或 C#。

如何压缩 - 并不重要。只需能够单独选择每个/对每个执行命令即可。

有什么想法吗?

Visual Studio Templates has folder structure like this:

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

I want to start at the root folder, dig down to the lowest subfolders and zip each of them to a separate archive.

Using Windows Batch or C#.

How to zip - doesn't matter. Just be able to select each separately / execute a command against each.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东走西顾 2024-11-07 19:55:56

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 来决定要压缩哪些目录

C# 4.0:

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

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

Look at ICSharpZip to do the actual zipping

Update:

After @khachik's suggestion my criteria should be di.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)

Ok, just mixing the condition with the Where()

as far as template folder can contains some subfolders, e.g. ASP.NET MVC web app

you can always provide a Func<DirectoryInfo, bool> to decide which directories are toBeZipped

迟到的我 2024-11-07 19:55:56

如果您使用 C#,则:

...如果您确实想要unzip,请尝试在 .net 中以编程方式解压缩文件

If you are using C# then:

... and if you actually want to unzip, then try Unzip files programmatically in .net

只是一片海 2024-11-07 19:55:56

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

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

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

So, you essentially want to find all sub directories that in turn has no sub directories?

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

Of course there will be issues if there are sub directories to which the current user does not have access and such, but I get the feeling that this code is for some convenience tool that will operate in a controlled environment.

停顿的约定 2024-11-07 19:55:56

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

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

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

This code below recursively goes through a directory tree. You should be able to use it as a basis for your code:

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

You can just return a bool if there are no more directories to go down and then zip all the files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文