FileBots — Quick, File Processing Tools

FileBots transform files quickly, on-the-fly from the comfort of your very own Windows Explorer.

Occasionally, I’ll need to transform text files that I’m working. This will usually happen when I’m porting over old, ASP-based applications to ASP.NET or converting from VB.NET to C#—that sort of thing.

Recently, I was converting over an old site that had HTML in all uppercase. This used to be the standard. But, as you know, XHTML requires everything to be in lower case.

To solve the problem, I really didn’t need anything fancy. I wasn’t trying to create anything perfect. No need to employ DOMs or YACC and such. I just wanted to crank out something real quick that would convert the majority of the HTML as I needed it. There’s probably even a plugin to VS for this but I didn’t look. If you find one, let me know. VS doesn’t do this for me that I could find.

So, to make my life easier with as little effort as possible, I created a console app (.EXE) that takes the first argument as the name of the file. In the console application, I just load the file, do the processing and then save the file with a ".actionTaken" extension. Done! I call these little applications FileBots.

Let’s look at at a FileBot a little closer. Here’s the Main() routine from a recent FileBot that I created that lowercases the elements and attributes in an HTML file:

static void Main(string[] args)
{
    if (args.Length == 1)
    {
        File.WriteAllText(args[0] + ".lower", LowerCaseHtml(LowerCaseAttributes(File.ReadAllText(args[0]))));
        Process np = new Process();
        np.EnableRaisingEvents = false;
        np.StartInfo.Arguments = args[0] + ".lower";
        np.StartInfo.FileName = "notepad.exe";
        np.Start();
        np.Close();

    }
    else
    {
        Console.WriteLine("Usage:");
        Console.WriteLine("");
        Console.WriteLine(" ConvertUpperHTMLToLowerHTML.exe filename");
        Console.WriteLine("");
        Console.WriteLine("Outputs filename.lower");
    }
}

Example 1: C# Main for a FileBot

Pretty straight forward, yes?

"Yeah, but you just created a console application! How is that going to make things easier for me?" you might say. Well, here’s the sweet part about Windows. Drop this .EXE in to your SendTo folder located here:

C:\Documents and Settings\LoginName\SendTo

Now, when you right-click on a file, you’ll see that the .EXE shows up in the Send To menu as a valid option. When you choose the program from the menu, the Windows explorer executes the application with– you guessed it–the file name as the first argument!

In the example above, you’ll notice that in just one line of code, I read the file, process it and write it back down. I don’t even bother with error handling. No need.

In the code that follows, I start notepad.exe and pass the output filename to it. You don’t have to open the file in notepad. You can do whatever you want including not doing anything.

This is really useful I think. Take it one step further. You can select the previous output filename and then send it to another FileBot until you get the final results you’re looking for. This is similar to the UNIX and MS-DOS pipe without having to go to a command console and chain them together.

The key is, when developing, don’t try to over engineer your scaffolding code. Be smart. Use the OS and the development environments to help you move quickly. And, I sure as heck had a lot more fun creating the above than searching and replacing HTML in hundreds of files. Take a second every now and then to add a tool to your chest. It pays off in the long run.

Now, go make some FileBots!

2 thoughts on “FileBots — Quick, File Processing Tools

  1. You should publish this utility on http://www.codeplex.com/ or some other open source site. This would help others find it and potentially put it to use.

    Small utilities like this are many times lost because no one thinks that making them available is worth the trouble, i mean anyone can make it?… right? I dont think the correct answer is yes as i have seen many people struggle when simple solutions existed.

Comments are closed.