views:

433

answers:

5

I would like to write simple scripts in C#. Stuff I would normally use .bat or 4NT .btm files for. Copying files, parsing text, asking user input, and so on. Fairly simple but doing this stuff right in a batch file is really hard (no exceptions for example).

I'm familiar with command line "scripting" wrappers like AxScript so that gets me part of the way there. What I'm missing is the easy file-manipulation framework. I want to be able to do cd(".."), copy(srcFile, destFile) type functionality.

Tools I have tried:

  • NANT, which we use in our build process. Not a good scripting tool. Insanely verbose XML syntax and to add a simple function you must write an extension assembly. Can't do it inline.

  • PowerShell. Looks great, but I just haven't been able to switch over to this as my primary shell. Too many differences from 4NT. Whatever I do needs to run from an ordinary command prompt and not require a special shell to run it through. Can PowerShell be used as a script executor?

  • Perl/Python/Ruby. Really hate learning an entirely new language and framework just to do batch file operations. Haven't been able to dedicate the time I need to do this. Plus, we're a 99% .NET shop for our toolchain and I really want to leverage our existing experience and codebase.

Are there frameworks out there that are trying to solve this problem of "make a batch file in C#" that you have used?

I want the power of C#/.NET with the immediate-mode type functionality of a typical cmd.exe shell language. Am I alone in wanting something like this?

+23  A: 

I would try to get over the PowerShell anxiety because it is the shell of the future. All of the software coming out of Microsoft is using it as their management interface and especially version 2.0 is ridiculously useful.

I'm a C# developer most of the time but PowerShell has solved that whole "WindowsApplication42" problem of temp projects just piling up. PowerShell gives you full access to the .NET framework in a command line shell so even if you don't know how to do something in PowerShell, you most likely know how to do it in .NET.

Josh Einstein
well said and totally agree.
J.W.
+1 on Powershell 2.0 being so useful. The included ISE is worth it alone.
Jason Baker
Sold. Your answer and John Robbins's blog has finally convinced me. :)http://www.wintellect.com/cs/blogs/jrobbins/archive/2008/02/17/eight-weeks-of-powershell.aspx
Scott Bilas
For what it's worth, if you have any questions about PowerShell or gripes or whatever, feel free to email or IM me. I'm all for preaching the Gospel of PowerShell. :)
Josh Einstein
+3  A: 

IronPython and IronRuby do let you leverage all of your .NET "experience and codebase" (they don't answer your objection to learning new languages, however).

Alex Martelli
+2  A: 

If you have any bash nerds, you can always try cygwin.

Also remember that Python was originally intended as a "glue" langauge. If you used the aforementioned IronPython, it's pretty easy to tie together pre-written C# classes.

Jason Baker
Bash or any of those shells on Windows is really hard. Have tried many times and I keep getting screwed on the / vs. \ issue. :( So it sounds like IronPython and Powershell are the best options so far.
Scott Bilas
+1  A: 

If you are bind to MS, PowerShell is surely the way to go. But I don't like it much.

I personally use MSBuild script more, and would like to see Mono C# Shell one day comes to Windows.

Lex Li
What is "MSBuild script"? Do you just mean the XML stuff? Because it's super verbose, hard to debug outside of printf-debugging, difficult to extend...But thanks for the reference to Mono C# Shell! I didn't even think to check Mono but it makes sense they'd have something like this. The gui is apparently not going to work on Windows but the command line version should be totally portable. It won't solve the problem I have in this question (batch scripts) but it looks really useful in general. I'll check it out, thanks again!
Scott Bilas
I don't need a huge debugger for MSBuild scripts, so I choose this one, http://blogs.msdn.com/parthopdas/archive/2007/12/01/visual-debugger-for-msbuild-projects.aspx
Lex Li
+1  A: 

I think CS-Script might be the ideal solution for you.

Dimitri C.