CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo [MVP]

Software management consultant and CTO, Headspring Systems

C# Scripter: How to execute C# as batch files with my new utility - level 200

UPDATE:  This utility has been upgraded to support more .Net languages.  See here for the newer version.

The Windows shell has some great programming capabilities, and we can do a lot with it, but these days, I can whip something out with C# and basically do anything I need to with files.  It takes me considerably longer to do the same thing with DOS commands (mostly because I don't know advanced shell scripting).  When Monad comes out in the next version of Windows, we'll have .Net integrated into the shell, but that is many years away.  I'm left wanting to execute C# batch-style.

It's no problem to create a console app and run it, but I want to have the .cs file available for tweaking, so it's not feasible to just create a bunch of .exe files.  I needed a true C# scripting solution.

I now introduce the Palermo C# Scripter.  On the command line, it's csscripter.exe, and it takes as an argument the name of the C# file.  For example:  csscripter.exe myBatch.cs

myBatch.cs just has be a valid console class like the following:

    1 using System;

    2 using System.Collections;

    3 using System.IO;

    4 using System.Windows.Forms;

    5 

    6 public class Whatever

    7 {

    8     public static void Main(string[] args)

    9     {

   10         Console.WriteLine("Other Script file executed. " + string.Join("|", args));

   11         MessageBox.Show("You can even do pop-ups", "With a title");

   12     }

   13 }

I'm using this now to traverse thousands of nested files searching for little things and doing small things when I find something.  This is the same stuff I'd do with batch files, but now I have the power of C# in text form doing it for me. 

You can download the utility from me here.

If you have feedback or a feature request, please leave me a comment.  In putting this together, I came across some good references on C# scripting:



Comments

Jeffrey Palermo said:

If there is interest in the code, I'd be willing to open-source it. Just leave me a comment.
# October 7, 2005 7:28 PM

Rob Bazinet said:

Nice utility! I have a need for something like this right now and was going to write one. I would be interested in the source for sure, it would certainly save some time and provide some enhancements too.
# October 7, 2005 7:53 PM

Jeremy D. Miller said:

The very first thing you should do with C# Scripter is apply it to a certain codebase Monday morning so our solution doesn't contain 50 projects anymore.
# October 7, 2005 9:55 PM

Brendan Tompkins said:

Oh, this is very cool!
# October 8, 2005 11:28 AM

Jon Galloway said:

Very cool. It's always bugged me that I program in c# and script in VBS.

How would you compare this to NScript (http://www.codeproject.com/dotnet/nscript.asp)?
# October 8, 2005 8:22 PM

Jeffrey Palermo said:

I haven't compared it to NScript, but it solves the same problem. I believe one advantage I have is that it's just a stand-along executable. You don't have to execute the .msi to install it on the machine. You just need the .EXE to use it (much like NAnt, NUnit, etc). I don't claim that this is an orginial idea, and there have been others that wrote a similar tool (see the links in my post).
# October 9, 2005 11:39 AM

Christopher Steen said:

AJAX and ASP.NET discussion list [Via: Wallym ]
ASP.NET Podcast #22 - Interveiw with Shanku Niyogi...
# October 9, 2005 9:55 PM

Daniel F said:

Ooh, nice. Very nice! I can already think of a few things I've been putting off simply because I don't want a full solution and exe floating around just to do simple things.

Try not to point and laugh at the mort, but... any chance it can do .vb files?
# October 9, 2005 10:44 PM

Jeffrey Palermo said:

Daniel,
No, this is C# -specific. It invokes the C# compiler internally.
# October 9, 2005 11:08 PM

Daniel F said:

hehe, no probs. Guess I'd better brush up my c# skills to get stuff done. Thanks for the quick reply, much appreciated :)
# October 9, 2005 11:21 PM

Andy said:

Depending on how you implemented it, I don't think that it would be very hard to add VB support. If you use CodeDom to compile the the passed in file, I think all that would need to be done is to create an instance of the correct CodeProvider (VB vs C#) based on the file that was passed in. Maybe the code could decide based on the file extension?
# October 10, 2005 9:52 AM

Jeffrey Palermo said:

Andy,
Yes, it definitely could be made to compile VB as well. I'll have to think about adding that. I have no use for that, but other people may.
# October 10, 2005 9:56 AM

Jeffrey Palermo said:

I've modified the scripter so that it now supports VB and JScript. I'll have to rename it to netscripter.exe. I'll post it soon.
# October 11, 2005 9:31 PM

Jeffrey Palermo said:

Daniel,
I've extended the scripter to support VB.Net as well. Let me know what you think.
http://codebetter.com/blogs/jeffrey.palermo/archive/2005/10/12/133007.aspx
# October 12, 2005 12:14 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication