Simple .NET Multiple File Downloader
13-Jul-07
Jeff Atwood has completely taken over my life.
Well, perhaps not completely, but for whatever reason over the past week or so I've been reading a whole lot of posts from his archive (and by "a whole lot" I mean "every single") whenever I'm waiting for my computer to do something else. For a VB.NET developer (ha, take that, Jeff), he's pretty cool.
In any case, in one of his posts he linked to the (freely) downloadable version of the Graphics Programming Black Book by Michael Abrash, which I'm somewhat interested in poking through (as time allows). So I innocently click and am promptly presented with 70+ links to the individual pdf for each chapter.
Oh. Joy.
As you can imagine, I wasn't particularly interested in point-and-click downloading that many files (can you say RSS?), so I whipped up a quick little console app to download all the files, using Jeff Key's abso-f*&king-lutely genius Snippet Compiler (dotnet 2.x). Which, if you program in .NET (C#, that is), I suggest you download. Now. Before caring about anything I do or don't have to say.
Code for the downloader (Snippet Compiler's default helper methods included). Because I like sharing.
-
using System;
-
using System.Collections.Generic;
-
using System.IO;
-
using System.Net;
-
-
public class MyClass
-
{
-
public static void Main()
-
{
-
string urlTemplate = "http://www.byte.com/abrash/chapters/gpbb{0}.pdf";
-
string saveLocation = @"C:\\Graphics Programming Black Book\Chapter_{0}.pdf";
-
-
int fileCount = 70;
-
for( int i = 1; i < = fileCount; i++ )
-
{
-
Console.Write( "downloading chapter {0}", i );
-
-
string url = String.Format( urlTemplate, i );
-
string filePath = String.Format( saveLocation, i );
-
try
-
{
-
client.DownloadFile( url, filePath );
-
Console.WriteLine( " => done." );
-
}
-
catch( Exception e )
-
{
-
Console.WriteLine( " => error: {0}", e.Message );
-
}
-
}
-
Console.WriteLine( "all done" );
-
-
RL();
-
}
-
-
#region Helper methods
-
-
private static void WL(object text, params object[] args)
-
{
-
Console.WriteLine(text.ToString(), args);
-
}
-
-
private static void RL()
-
{
-
Console.ReadLine();
-
}
-
-
private static void Break()
-
{
-
System.Diagnostics.Debugger.Break();
-
}
-
-
#endregion
-
}
Yes, I know there are probably Firefox plugins that would've done this (not to mention download managers), but I couldn't be bothered to try and find one. Besides, this way was more fun.
And yes, I know that this code is completely custom for downloading this particular series of files, but it could be easily and quickly modded to handle multiple scenarios (have the user enter a format, use a text file full of urls, etc, etc). The point here was fast.
And no, there's no progress indication. Deal with it. I did.




