代码之家  ›  专栏  ›  技术社区  ›  Bergius

使用PowerShell进行筛选和复制

  •  0
  • Bergius  · 技术社区  · 16 年前

    为了提高我的PowerShell技能,这里有一个简单问题的丑陋解决方案的例子。欢迎就如何改进oneliner提出任何建议。

    任务:将一个巨大的图标库修剪成更易于管理的东西。原始目录结构如下:

      /Apps and Utilities
        /Compile
          /32 Bit Alpha png
            /Compile 16 n p.png
            /+ 10 or more files
          /+ 5 more formats with 10 or more files each
        /+ 20 or so icon names
      /+ 22 more categories
    

    我想复制32位Alpha pngs并稍微扁平化目录结构。这是我快速而非常肮脏的解决方案:

    $dest = mkdir c:\icons; gci -r | ? { $_.Name -eq '32 Bit Alph
    a png' } | % { mkdir ("$dest\" + $_.Parent.Parent.Name + "\" + $_.Parent.Name); $_ } | gci | % { cp $_.
    FullName -dest ("$dest\" + $_.Directory.Parent.Parent + "\" + $_.Directory.Parent) }
    

    不好,但它解决了我的问题。最终结构:

      /Apps and Utilities
        /Compile
          /Compile 16 n p.png
          /etc
        /etc
      /etc
    

    你会怎么做?

    1 回复  |  直到 15 年前
        1
  •  0
  •   Goyuix    16 年前

    三条评论:

    • 在第一次测试后,你并没有对结果进行隐式排序。这可能没关系(它们可能无论如何都会排序),但因为您正在基于Parent构建目录结构。起源。..我可以想象一个PNG图像列表,这会导致某种问题(无意双关)。
    • 也许我过时了,但我发现 目录 可读性比 gci 。在我自己的脚本中,我通常会尝试扩展大多数别名,因为它往往能提高PowerShell新版本的可读性和可维护性。
    • 与其将所有内容链接在一起,不如将块分解为变量并将其封装在cmdlet/函数中。
    推荐文章