Saturday, May 24, 2008

Delete undeletable file with Powershell

I just came across a situation where I wanted to delete a file in a folder and I couldn't do so because the file was allegedly in use. I still needed to delete this file and tried a few things but was unsuccessful.

I dropped into Powershell and tried the Remove-Item call on the file which failed as well. I then tried the following sequence which worked like a dream:

$item = Get-Item FileToDelete.dll
$itemInfo = New-Object System.IO.FileInfo $item

And that was it, the file is now gone.

Friday, May 23, 2008

Fiddler trick

Scott Cate just posted an interesting usage to get Fiddler to work on localhost here Working with Fiddler on LocalHost. The short of it is to use but add an extra period to the end of it as in and if working on Cassini add the port number (with the colon) after that last period. I recall having this problem in the past and now I have a solution for it. Thanks Scott!

Thursday, May 22, 2008

Powershell Get-ChildItem - FileSystemInfo or Array

I've just discovered something interesting in my ventures into Powershell. If a directory has a single file in it then the Get-ChildItem in that directory will return a System.IO.FileSystemInfo object but if there are 2 or more files then it will return a System.Array object.

I'm running a script and in the script I'm getting a count of the number of files in each of several directories. So what I've found that I have to do is to check the value of the "count" member return and if it's null then I assume that the object is a System.IO.FileSystemInfo object and call the Directoy.GetFiles().count member to get the number (1) of files in there.

This is what that little snippet in my Powershell script looks like now:

        $fileCollection = Get-ChildItem $s
        if($fileCollection -eq $null)
            $fileCount = 0
            $fileCount = $fileCollection.count
        if($fileCount -eq $null)
            # This happens if a directory has 1 file in it. Instead of receiving an Array object back
            # from the Get-ChildItem call we receive a System.IO.FileSystemInfo object and we need
            # to call the Directoy.GetFiles().count on that to get the right value.
            $fileCount = $fileCollection.Directory.GetFiles().count
        $totalFileCount += $fileCount

The length of a multi dimensional array

This is a note to myself so that I'll remember how to get the length of a single dimension of a multi dimensional array in C#.

string[,] array = { {"a", "a", "a"}, {"b", "b", "b"} };

// Returns 2

// Returns 3

Saturday, May 3, 2008

Unit Testing Production Exceptions

I have a web site that gets a bit of traffic and I log exceptions on that site. Nothing exciting or special, a sort of Log4Net but home grown and speciliazed for the site. The log recently showed an index out of range exception. When I saw the exception I kicked myself for not having the foresight to also log all the options, parameters and data that the user had input so that I could reproduce the error. This got me thinking...

How about another or supplemental log that stored structured unit test data from the production environment? An exception happens in the production environment but it's an exception that's in a function that is directly unit tested in the development environment. During exception logging you write an XML fragment with all the relevant data to your log, supplemental log or DB.

When it comes to running unit tests you could have a setup script pull all unit test data from the production logs and insert it into the data store that's used for unit testing. You'd obviously have to do some checks to make sure that you don't have duplicate data as this is likely to happen if many users enter the same data that generates the same exception. Filtering this during the setup import would be the best place to cull the duplicates.

I've done a partial implementation of this. My log now has detailed input data and I currently copy/paste this into the unit test data store. Next step is to come up with a clever pattern to allow me to pull data for different types of unit tests. To be continued...

RSS Feed

Someone recently asked me where the RSS Feed for this site is. I'm going to have to add the link in an obvious place on the home page at some point soon but in the meantime here it is:
RSS Feed

Hansel Minutes

I recently downloaded 109 podcasts from HanselMinutes using Powershell. Today was the first day that I started listening to them on my ride to work. I cycle to work and it takes me around 40 minutes each way which is about how long each podcast lasts. This means that I'm going to be able to listen to 10 podcasts a week and so it'll take me 10.9 weeks to listen to all of them. However, by then there should be another 11 podcasts available so it's going to take me a further 1.1 weeks to listen to those and then another day for the one produced during that week. There must be a formula that deals with this decreasing recursion problem - I don't know what it is.

The good news is that I really enjoyed the first 2 podcasts. I'm surprised at how quickly they've become dated. In the first podcast Scott Hanselman discussed the new XBox 360 Live which seems so pase now. However it is packed with excellent geek content and I appreciate his attitude to podcasting which is to provide a ton of content in a short period of time with little or no blathering. Excellent work Scott!