Wednesday, July 12, 2006

List files based on number of lines.

1I was working on a javascript error in IE which returned the line number of the error but not the file where the error occured. The page was accessing half a dozen seperate js files. This particular page doesn't do to well in FireFox, so using Venkman was out. However, there was something unusual about the error. The line number was 1202. I thought to myself, "How many js files could we possibly have with more than a thousand lines of code?" It actually isn't that many. But how to find this out? I'll tell you how! Powershell!

I opened up a powershell console, changed the directory to the directory where we keep all the js files, and typed in this little ditty (after several revisions, of course).
ls *.js | where {(gc $_).length -gt 1200}
Which returned:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/11/2006 2:12 PM 53520 JScript.js
-a--- 6/15/2006 1:43 PM 43806 StoryDescription.js
-a--- 7/11/2006 2:12 PM 88027 StoryDescriptionEdit.js

Let's look at what just happened here. At first glance, you'll notice that the line is pretty terse. That's because I used predefined aliases for the functions that I called. In PowerShell, every command that you call is actually a small application known as a commandlet. PowerShell also lets you alias these commandlets, in order to save you some time. There are many predefined aliases. To see a list of the aliases, just type "alias" at the prompt. If you pass in an alias then it will limit the results to that alias name. So if you type in "alias ls", you'll see that you're actually calling Get-ChildItem. It has just been aliased to make it easier for Unix/Linux users. It is also aliased as "dir". In fact, the entire line could be rewritten without any aliases as
Get-ChildItem *.js | Where-Object { (Get-Content $_)
.length -gt 1200 }

So the first little bit of the string returns a list of all the files in the current directory that have a ".js" extension. Then the pipe symbol, "|", passes the results to the next code statement. The where-object commandlet acts like a filter and only returns the items that meet the specified criteria. In this case we're making sure that the number of lines in the file is greater than (-gt) 1200.

We determine the number of lines with the Get-Content commandlet. This commandlet returns an array of all the lines in the file. You can even loop through the array, but that's another blog post. We just need to get the length property off of the array object.

And there you have it. Only three files in the directory have more than 1200 lines of code and my page only uses one of them. Even though I'm just a newbie, I'm loving PowerShell. Unlike most of my peers, I refuse to believe that it is only for system admins.

No comments: