Huddled Masses
You can do more than breathe for free...
Browse: Home / 2008 Scripting Games – Solutions for Beginner Events 3 and 4

2008 Scripting Games – Solutions for Beginner Events 3 and 4

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

In my ongoing chronicle of solutions to the 2008 scripting games events … the task in beginner’s event 3 was to copy the first line of each text file in a folder into a new text file. This task is so trivial in PowerShell that my solution is the same as the Scripting Guy’s:


get-content C:\Scripts\*.txt -total 1 | set-content C:\Temp\Newfile.txt

If you need that explained … well, you’re just going to have to read their explanation, because quite frankly, it’s too boring to get into.

Beginners Event 4

For beginner’s event 4 the task was to read in the content of the script file itself and count the characters. This is actually just as easy as the last one, but somehow, the scripting guys’ solution went off into vbscript loop land again. They actually loop through each line and manually add up the line length. My script is 55 characters long ;-) and looks like this:


gc $MyInvocation.MyCommand.Path|Measure-Object -C|fw C*

Slick, right? Ok, I’ll explain that one. $MyInvocation is a built-in variable which contains everything there is to know about what’s running and how it was started. $MyInvocation.MyCommand contains the information about the command that’s running, it’s .Path if it’s a script, and so on. On the other hand, $MyInvocation.InvocationName contains the first thing on the command-line, and so even though the scripting guys used it, you should never assume InvocationName will contain the script path — it would fail if the script had been run by dot-sourcing, or by invoke-expression or by the invoke character & or … well, you get the idea, it would fail often.

Anyway, once you have the path of the script you get-content (gc is one of the built-in aliases for that) and pipe it into the Measure-Object cmdlet which has a -Characters parameter. As always, when passing parameters, you only need to pass enough to differentiate the parameter from others.

In normal use, that would be enough: the output of Measure-Object is simple and very clear. For the scripting games, we want to be a little more precises, so we pipe the output to the Format-Wide cmdlet using it’s built in alias “fw” — unlike Format-Wide and Format-List, Format-Wide won’t add headers, so it outputs just the number. However, we have to specify which property to show, but since there’s only a few, we can use a wildcard match to select the Characters and a single letter is enough to differentiate from the other properties: Lines, Words, and Property. And that’s it!

Similar Posts:

  • Parenthesis in PowerShell
  • Are you interested in a virtual PowerShell brown-bag event?
  • What Scope Am I In?
  • The problem with calling legacy/native apps from PowerShell
  • PowerShell Scripting Best Practices: Prefix A

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.