Huddled Masses
You can do more than breathe for free...
Browse: Home / 2008 Scripting Games – Solutions for Beginner Event 2

2008 Scripting Games – Solutions for Beginner Event 2

By Joel 'Jaykul' Bennett on 20-Feb-2008

The task in Beginner’s Event 2 was to list out how a computer’s installed True Type fonts, and print a count of them (and of the total fonts). This really isn’t worth explaining, because it’s so simple, but again, the Scripting Guys Solution uses these extremely wordy mechanisms that make everything seem like so much work … I felt like I had to post a couple of alternate solutions. Lets start with the cleanest. Oh, and by the way, the code reads a lot better if you click through to the wider single-article layout of my site. ;-)


# get all the font names from the registry as required, but I'm lazy,
# so I use *s and let the computer figure the path out
$fonts = (get-item "HKLM:\Soft*\Microsoft\* NT\Cur*ion\Fonts").GetValueNames()

# But we only want the true type ones
$ttf = $fonts -match "TrueType"

# Now print everything out:
$ttf
""
"TrueType: " + $ttf.Count
"Total: " + $fonts.Count

Notice the total lack of the statement for or foreach? That’s a beautiful thing. My favorite part is the line $ttf = $fonts -match "TrueType". Filtered array assignment works with any comparison operators, it’s very nice. Please use it. And yes, if all you wanted was to print out the TrueType font names, this one-liner would suffice:


(get-item "HKLM:\Soft*\Microsoft\* NT\Cur*ion\Fonts").GetValueNames() -match "TrueType"

Ok, for those of you who just can’t stand not having counter variables … do yourself a favor, and at least learn the syntax for switch -regex:


$ttf=0; $fonts=0;           # you don't have to zero these out if they're already empty

switch -regex ((get-item "HKLM:\Soft*\Microsoft\* NT\Cur*ion\Fonts").GetValueNames()) {
  "TrueType" { $ttf++; $_ } # for true type fonts, increment the counter and print it out
  default {$fonts++}        # for everything else, just increment the counter
}
""
"TrueType: " + $ttf
"Total: " + $ttf + $fonts

Similar Posts:

  • Better error messages for PowerShell ValidatePattern
  • ShowUI: Handling Events and Producing Output
  • PowerBoots 0.3 – The Faster Edition
  • Parenthesis in PowerShell
  • PowerShell 3 – Finally on the DLR!

Posted in Huddled | Tagged 2008 Scripting Games, PowerShell, Scripting, Solutions

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010
  • August 2010

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.