Filtering and copying with PowerShell

Posted by Bergius on Stack Overflow See other posts from Stack Overflow or by Bergius
Published on 2009-01-26T14:17:25Z Indexed on 2010/05/04 0:48 UTC
Read the original article Hit count: 293

Filed under:
|

In my quest to improve my PowerShell skills, here's an example of an ugly solution to a simple problem. Any suggestions how to improve the oneliner are welcome.

Mission: trim a huge icon library down to something a bit more manageable. The original directory structure looks like this:

  /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

I want to copy the 32 Bit Alpha pngs and flatten the directory structure a bit. Here's my quick and very dirty solution:

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

Not nice, but it solved my problem. Resulting structure:

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

How would you do it?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about one-liner