Sunday, December 2, 2007

Counting # of matching lines in file

I wanted to scan a large number XML files and determine which ones had more than 20 elements of a particular type. Here was the PowerShell command-line I used to report on files that contains more than 20 "" elements:

gci *.xml |% {$fn=$_.name; gc $_ | where {$_ -match ""} |% {$count = 0}{++$count}{if ($count -gt 20) {$fn}}}

An easier, but not very good for performance if a large number of files are involved is:

gci f_*.xml | select-string "" | group filename | where {$_.count -gt 20} | select count, name

Tuesday, November 6, 2007

PowerShell to report top 10 results from log file

Ever want to sift through a log file and report on the top 10 occurances of a certain field value? Here is a PowerShell script that will do just that.

I had a log file that contained an IP address in the third field (column index 2 since arrays start at zero in PowerShell). I wanted to know what were the top 10 IPs that were logs. I could call this script like:

./ipcount.ps1 logfile.log 2

When using huge log files, don't forget if you want to redisplay, but not recompute, the results, you can "dot source" the script like:

. ./ipcount.ps1 logfile.log 2

Then $result will always hold the last set of results. Here is the script (3 lines...middle line is really long):

param($file,$index)

$result = gc $file | foreach {$hash=@{}}{$hash[$_.split(',')[[int]$index]] += 1}{$hash.getenumerator()} | sort value -desc | select -first 10

$result

Friday, November 2, 2007

URLs and System.IO.Path

Cool. I just found out that the .NET System.IO.Path static methods like GetFilename and GetDirectory work with internet URLs as well. I was wanting to get just the file name from a URL and the System.Uri class doesn't do this...but Path does. Now ain't that slick.

Friday, October 26, 2007

Finally...a use for .NET partial classes

When .NET 2.0 was released, partial classes was recognized as one of the top four cool new features (along with generics, anonymous methods, and iterators). After developing with 2.0 for some time now, I haven’t found a good reason to use partial classes (besides the Page class in ASP.NET that is created for you automatically). I do however recognize its importance for Visual Studio 2005 and how the auto-generated code and our own Page classes are created. Besides that, I just haven’t ran into a situation where I really needed it.

Recently I wrote some code where I stood up and cheered partial classes. If you ever used the .NET xsd.exe utility in .NET 1.1 to create a strong-typed class for an XSD file, you know that the tool created this handy set of classes for you to access the elements of an XML file. This was a good thing, but I would always end up writing several separate utility classes to provide a layer on top of, or along side of, the auto-generated class from xsd.exe. Although tempted at times, I couldn’t modify the auto-generated class file since I might regenerate it at a later time (I always did).

Much to my glee, xsd.exe in .NET 2.0 now creates partial classes! This now allows me to write separate class files and add properties and methods to the auto-generated ones from xsd.exe. This provides a nice separation/coupling from the xsd.exe generated code. Excellent!

I see a pattern here…for things that auto-generate code, partial classes are a welcome addition to .NET.

Friday, October 19, 2007

DataFormatString not working in GridView

I often use the DataFormatString in the .NET 2.0 GridView control. It seems like the DataFormatString property on a bound column is ignored unless you also set the column's HtmlEncode property to false (the default value is true).

.NET ToString() formatting help

Here is a useful 2-page cheat sheet on the various ToString() formatting options in the .NET Framework languages.

Download Here

Wednesday, October 17, 2007

Clear Visual Studio "Recent Project" entries

Sometimes you want to clear all the "Recent Projects" from my Visual Studio Start Page. You can do this by removing registry values at the following locations in the registry:

To clear recent files:
HKCU\Software\Microsoft\VisualStudio\8.0\FileMRUList

To clear recent projects/solutions:
HKCU\Software\Microsoft\VisualStudio\8.0\ProjectMRUList

Saturday, October 13, 2007

Regex.Replace

From Brian Davis:

The String.Replace function has been a valuable tool for years, allowing programmers to change strings by replacing a specific substring with another substring. While this is usually enough, the Regex.Replace function goes a step (ok, maybe 10 steps) further. It allows replacement of text using regular expressions. Not only can you define the text to replace using a regular expression, you can define what to replace it with using a replacement string containing special constructs which identify portions of the mathed text. A common example is the representation of a name. Let's say you have a name in a string like "John Doe", but you would really like to have "Doe, John". You could accomplish this with a few lines of code, splitting the string on a space and constructing a new string using the elements, or you could do it in one simple line:

strInput = Regex.Replace(strInput,"(?\S+) (?\S+)","${last},${first}")

This is a simplified example with a simple expression (we don't allow names like John J. Doe, John Boy Doe, John Doe, Jr., etc.), but it shows how using the Regex.Replace function can take several lines of code down to just a few. If we were to write a more complex expression, it would still require just one line of code in our application, whereas we may have to add dozens of lines of code with If...Else blocks or Select Case statements to accomplish complicated parsing.

Here's a look at some of the constructs that can be used in a Regex replacement string:

$& - matched text
$_ - original source string
$` - text before match
$' - text after match
${group_name} - text matched by named group
$1, $2 - text matched by numbered group
$$ - the literal "$"

Tuesday, October 2, 2007

Creating a type of Type based on a string

I recently ran into a case where I had a string value like "System.String" and I needed to determine what type this represented, then assign that to a member of type Type.

Type t = Type.GetType("System.String");

Tuesday, September 25, 2007

Changing to the root directory with FtpWebRequest

Extracted from: http://blogs.msdn.com/mariya/archive/2006/03/06/544523.aspx

Many customers ask us how they can use the CWD command with our FtpWebRequest.

The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.

Let's say you're using the following format:

String uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl";

FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(uri);

Request.Method = "LIST";

The above example will bring you to your user's directory and list all the contents there. Now let's say you want to go 2 directories backwards and list the contents there (provided your user has permissions to do that). You close the previous FtpWebRequest and issue a new one with this uri

uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/%2E%2E/%2E%2E";

This is equivalent to logging in with your user's credentials and then using cd ../../

Note: if you try using the ”..” directly without escaping them the uri class will strip them, so "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/../.." is equivalent to "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/"

Now let's say you want to go to another user's directory which is one level above the root. If you don't specify a user name and password it's equivalent to logging in as anonymous user. Then you issue a new FtpWebRequest with the following uri

"ftp://myFtpUrl/%2F/anotherUserDir"

This is equivalent to logging in as anonymous and then doing

Cd /

cd anotherUserDirectory

Monday, September 17, 2007

Binding XML data into a DataList, GridView, etc.

Here is a nice article on how to bind sections of an XML file (or string) into a DataList, GridView, etc. using the .NET 2.0 XmlDataSource. Basically, it is the same as binding a SqlDataSource. It is nice since you can bind a subset of the XML rather than the entire data.

http://aspnet.4guysfromrolla.com/articles/061307-1.aspx

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...