Filtering and copying with PowerShell -


To improve your PowerShell skills, here is an example of an ugly solution to a simple problem.

Mission: Trim a huge icon library for something a little more manageable. The parent directory structure looks like this:

 / apps and utilities / compile / 32 bit alpha png / compilation 16 n p.png / + 10 or more files / + 5 and more with 10 or more files Format / + 20 or more icon names / + 22 more categories 

I want to copy 32 bit alpha PNG and cover the directory structure slightly. Here's my quick and very dirty solution:

 $ dest = mkdir c: \ icons; GC-R | ? {$ _. Name-AQ '32 bit letter A PNG'} | % {MKDIR ("$ dest \" + $ _. Parent.Parent.Name + "\" + $ _. Parent.nam); $ _} | GC | % {Cp $ _} Full Name - Dest ("$ dest \" + $ _ directory. Parent.parent + "\" + $ _ directory. Parent)} 

Not good but my problem solved has occurred. As a result structure:

 / Applications and Utilities / Compile / Compilation 16 n p.png / etc / etc / etc 

How would you do this?

Three comments:

  • After you test your results, Are not sorted randomly. It probably does not matter (they will sort anyway) but since you are creating a directory structure based on parent. Parents ... I can conceive a list of PNG images which are some kind of problem
  • Maybe I'm old-fashioned, but I can read dir more than GI . In my scripting I usually try to expand most nicknames because it tries to improve readability and maintenance for those who are new to PowerShell.
  • Rather than connecting all things together, it is understood to break fragmentation into variables and conclude it in a CMDlet / function.

Comments