Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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:


Posted 10-07-2005 4:04 PM by Jeffrey Palermo

[Advertisement]

Comments

Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-07-2005 7:28 PM
If there is interest in the code, I'd be willing to open-source it. Just leave me a comment.
Rob Bazinet wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-07-2005 7:53 PM
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.
Jeremy D. Miller wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-07-2005 9:55 PM
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.
Brendan Tompkins wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-08-2005 11:28 AM
Oh, this is very cool!
Jon Galloway wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-08-2005 8:22 PM
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)?
Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-09-2005 11:39 AM
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).
Christopher Steen wrote Link Listing - October 9, 2005
on 10-09-2005 9:55 PM
AJAX and ASP.NET discussion list [Via: Wallym ]
ASP.NET Podcast #22 - Interveiw with Shanku Niyogi...
Daniel F wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-09-2005 10:44 PM
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?
Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-09-2005 11:08 PM
Daniel,
No, this is C# -specific. It invokes the C# compiler internally.
Daniel F wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-09-2005 11:21 PM
hehe, no probs. Guess I'd better brush up my c# skills to get stuff done. Thanks for the quick reply, much appreciated :)
Andy wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-10-2005 9:52 AM
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?
Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-10-2005 9:56 AM
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.
Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-11-2005 9:31 PM
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.
Jeffrey Palermo wrote re: C# Scripter: How to execute C# as batch files with my new utility - level 200
on 10-12-2005 12:14 PM
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