Saturday, April 26, 2008

Powershell Ripped Media Renaming Script

I recently bought a book on CD and ripped it to .wma so that I could listen to it on my portable media player. When the tracks rip to disk they get named with the track number starting first. I wanted to rename the files so that the two digit disc number preceded the track number so that I could sort all of the tracks in one folder and listen to them in the correct order. Here's a Powershell script that I ran from the folder one below the ripped discs' folders.

# change directory into root of ripped files
cd "D:\Audio\Author Name"
$dirr = "BookName Disc "
$discs = 1..7 # Change to number of discs

foreach($disc in $discs)
{
    $discdir = $dirr + $disc.ToString()
    cd $discdir
    $dir = get-childitem *.wma
    foreach($x in $dir)
    {
        $newname = "0" + $disc.ToString() + "." + $x.Name.SubString(0,2) + ".wma"
        Rename-Item $x $newname
    }
    cd ..
}

I've updated the code that appeared here before 8 July 2010. Previously it did not have the loop for each disc. This code still isn't perfect. Instead of prepending the 0 to the disc number when creating the new name I should be formatting the disc value.

Wednesday, April 23, 2008

Download List of Files from Web with Powershell Script

I recently came across Scott Hanselman's Hansel Minutes and it looked like the sort of thing that I wanted to listen to. At the time I found it the archives had 109 podcasts. I wanted to download them all and stick them on my MP3 player but didn't want to click through to each link and repeat myself 109 times. I'm a big fan of the DRY principal. So I wrote a Powershell script to do it for me.

First off I examined the file naming pattern:

http://perseus.franklins.net/hanselminutes_0001.wma

to

http://perseus.franklins.net/hanselminutes_0109.wma

Fantastically simple. You couldn't ask for a nicer pattern.

So here's the script:

function main()
{
    $clnt = new-object System.Net.WebClient

    $sourceNames = 1..109 |%{"hanselminutes_{0:0000}.wma" -f $_}
    foreach($s in $sourceNames)
    {
        $url = "http://perseus.franklins.net/" + $s
        $target = "c:\temp\" + $s
        write-host 'transfering from' $url 'to' $target
        $clnt.DownloadFile($url, $target)
    }
}

main

 

During the execution of the script over the 109 files that were in the archive at the time 3 files failed because of a timeout and 2 files failed because they didn't follow the naming pattern: The interviews with Jonathan Zuck and Robert Pickering (#'s 86 and 76 respectively) had their names tagged on to the file names.

Shrinking SQL Server Log File

I've been trying to work out how to do this for ages and today I've finally found the answer from Books Online. I think that a lot of us experience this. When you're searching the help files and resources you can't find the answer but later when you're not looking for it you stumble across it. For my notes, here is the example. Interestingly on most of my DB's I don't need full recovery so I've skipped the last step and left the DB's at simple recovery.

USE AdventureWorks;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE AdventureWorks
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (AdventureWorks_Log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE AdventureWorks
SET RECOVERY FULL;
GO

Thursday, April 17, 2008

Experts Exchange

One of the things that constantly frustrates me when looking for answers to programming questions via Google is that one of the top answers that will be listed will be on Experts Exchange but I can't get to see it because it's a paid site and I won't subscribe because I've heard too many stories about how the quality of the answers is worse than the answers given on free boards. Why pay for something that's worse than something that's free?

Jeff Atwood and Joel Spolsky have created a new web site called StackOverflow that is going to compete with sites such as Experts Exchange. This is fantastic news. These guys are talented individuals who write well and I'm sure will pull off a great site with good information and a great way to get to that information. Jeff is on the west coast and Joel on the east coast and they chat to each other weekly and record their conversations which appear as podcasts on the same site. Very cool!

Wednesday, April 16, 2008

Iterating through arrays in TDD

One of the patterns that I frequently follow in Test Driven Development (TDD) that I haven't seen mentioned elsewhere (yet) and that I find extremely effective is to create arrays of potential inputs for parameters to functions that I'm testing and then iterate through those arrays calling the same function and testing the results.

For example, if I had a function ConcatString(string one, string two) I could test it as such:

            string[] one = { "black", "blue", "orange" };
            string[] two = { "yellow", "white", "brown", "cotton" };
            for (int i = 0; i < one.Length; i++)
            {
                for (int j = 0; j < two.Length; j++)
                {
                    string actual = ConcatString(one[i], two[j]);
                    string expected = one[i] + two[j];
                    Assert.AreEqual(expected, actual);
                }
            }
 

Webmaster Day is April 29

April 29 is Webmaster Day. If you're a webmaster then send yourself an eCard. If you know a webmaster then send him/her an eCard. This year, Webmaster Day falls on a Tuesday.

Sunday, April 13, 2008

Deployed ASP.NET MVC app gives 404 on About Page

I am working through learning how to use the ASP.NET MVC framework with a web application. One of the annoying problems that I recently hit on was that the sample worked great when running under Cassini in the development environment but when deployed to IIS 6 I couldn't get to the About or Home page through the URL paths.

The following comment exists in the Global.asax.cs file:

            // Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
            //       automatic support on IIS6 and IIS7 classic mode

When running in Cassini you don't need to make this change. However, under IIS 6, if you forget to make this change then you'll hit this problem and get 404 page-not-found errors. Simply making this change appears to solve the problem.

I read some comments on the web that you need to add .mvc as a MIME type with a mapping of C:\WINDOWS\MICROSOFT.NET\FRAMEWORK\V2.0.50727\ASPNET_ISAPI.DLL which I tried initially with no success and then later went back and removed it when I got it working with the above solution. The MIME types don't appear to be the solution.

This was done with the MVC preview 2 released in Dec 2008.

Addendum:

I found that on Server 2003 Enterprise (Dev Machine) I did not need to map .mvc to C:\WINDOWS\MICROSOFT.NET\FRAMEWORK\V2.0.50727\ASPNET_ISAPI.DLL but when I deployed to Production (Server 2003 Standard) I had to go into the web site's properties and click Home Directory (tab) and then Configuration... (button) and then add the mapping. I thought that this was an Enterprise/Standard thing but then on closer inspection I discovered that the Enterprise machine already had this mapping. This probably happened when I installed the MVC preview onto the Enterprise (Dev) machine and it looks like (contrary to what I said before) you do need this mapping.

Addendum 2: (27 Oct 2008):

Since I've upgraded to asp.net mvc beta I've found that the above fixes don't work for me - they may still work for you. I've found the following resources useful so far:

Deploying ASP.NET MVC to IIS 6

Using ASP.NET MVC on IIS without the .MVC extension