What's a good way to write batch scripts in C#? - c#

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?

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.

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).

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.

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.

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

Related

What are some ways to compile Java from C# code?

I'm writing a multi-language IDE in C#. Right now, it works as a reasonably functional editor. However, I'm working now on adding the ability to compile and run Java code (I know it sounds silly to write this in C#, but I prefer using C#, and I need the Java support for a school class).
After that background, here is the question: What is the best way to compile Java code from C#? Right now, I am using the System.Diagnostics.Process class. Basically, I am invoking the compiler the same way you would call javac from the command line. Here is a rough example of my current implementation:
ProcessStartInfo buildInfo = new ProcessStartInfo();
buildInfo.WorkingDirectory = Path.GetDirectoryName(sourcePath);
buildInfo.RedirectStandardOutput = true;
buildInfo.RedirectStandardError = true;
buildInfo.UseShellExecute = false;
Process buildProcess = Process.Start(buildInfo);
I redirect standard output and error because later the application catches them to determine whether the compilation was successful. Is there a better way to do this? It feels a little sloppy, and I wanted to know if I was overlooking something.
You do have other options. Java provides JNI (Java Native Interface ) which allows Java to call native code and apropos, for native code to call Java (albeit in a rather complex way.)
Depending on how much of a learning experience you want this to be you can use JNI directly or use a library such as jni4net. A different interop approach is to use ikvm which is a jvm running inside the clr, but I don't think it'll be useful to you as it does not include a compiler.
You can also research alternative compilers such as gcj or ejc.
Not having tried to write an IDE I don't know whether these approaches are actually better than using the command line directly. My hunch is that for simple integration the command line is the easier to use however more complex scenarios, e.g. incremental compilation of large projects with multiple components, may require tighter integration.
If you plan on providing features such as inline debugging and error highlighting while you type you're going to require tighter integration anyway.
IDEs are extremely complex programs, even mere programming editors are complex enough.
A quick googling suggests (no surprise) there's no .Net API for compiling Java. Thus, the simplest way will probably be to use javac via the command line, as you are currently doing.
Your command line code looks similar to examples I've seen, so I don't expect there's much room for improvement. Thus, the short answer to your question: no, you're good (as good as compiling Java from C# can be, anyway).
You could use the Java Invocation API to host the JVM within your process and then run the compiler through the compiler API. It's probably faster than spinning up separate compiler process for compilation and certainly makes for a good learning experience (you'd need to use P/Invoke from the .NET side etc.).
There are some free and commercial tools out there:
http://community.versant.com/Blogs/db4o/tabid/197/entryid/94/Default.aspx
http://visualstudiogallery.msdn.microsoft.com/9789645d-9b31-4033-bcb1-53dc5ff58e05
And so forth...

C# or Java in command-line/scripting/interpreted environment?

So in Python on a unix environment, for example, we can open up python from terminal, start writing code and immediately run it to test some python library functions. Is there a similar commandline/scripting environment for C# or Java (perhaps a plugin to the IDE?)? Such a tool would seem pretty useful for experimenting with something like the System library.
Relatedly, I read that some versions of Python are compiled into bytecode before being executed by a virtual environment, which sounds like Java and C#. But, the Python program I open up in terminal and start typing code into has to be interpreted, right? So based on principle, it seems like it is possible to write a C# or Java interpreter. So I'm not sure why I just haven't seen anyone interacting with C# or Java on the commandline the way they do with Python.
Thanks.
For .NET there is LINQPad. It supports C#, VB.NET and F#.
The name suggests that it is only for LINQ but you can execute any code in there. There is no need for a database.
For .NET, if LINQPad is overkill for you, try the RunNET command-line package. It requires that you load script files instead of interactive typing, but lets you leverage more languages.
The ability to explore .NET types was one of the reasons I originally started learning F#, since it has an Interactive environment (in Visual Studio, click on View > Other Windows > F# Interactive.) Upon digging deeper into F# I have found it to be one of my favorite programming languages.
Anyways, you may want to look at it. It won't take long to figure out the basic syntax, and once you've got it you can explore various .NET objects in the Interactive Environment, if you don't want to wait for Roslyn.
For a tutorial, check out this page: http://www.tryfsharp.org/Tutorials.aspx

Build script with C#

For insight into the below, read: http://martinfowler.com/articles/rake.html
I've been using Rake (with Albacore) recently, and like it alot. Mostly the strength of having the Ruby language right at hand when I need it.
I must admit I am alot more at home with C# - and that lead me to thinking if there was an equivalent to Rake in the .NET world.
What I am looking for is a way to write build scripts in C#, or maybe a DSL, having the same dependency programming model, where I can also use C#.
Maybe I'm way off base asking this question. But if it's possible to do with Ruby (and an internal DSL), then I can't right off the bat say why the same wouldn't be possible for C#. And I certainly don't understand why it hasn't already been done :-)
Anyone have some perspectives on the issue?
What we already know:
C# needs to be compiled to run, so we would need to create a seperate build-script, that is parsed, and compiled at run-time by an executable.
Thanks in advance!
Solution
Use "Cake" http://cakebuild.net/ -- with Roslyn compiler what I wanted (years ago) is now possible. Thanks Microsoft. And thanks to the people who wrote Cake.
An alternative is to use Powershell. The library is called Psake (saké)
Maybe Cake is what you're looking for: https://github.com/cake-build/cake
A lot of people do use Rake for build scripts. There are even Rake tasks just for .NET. http://albacorebuild.net/
But there is a c# based make utility I know about. http://shake.codeplex.com/ And I thought I saw one on github. But I think they require a compile and that didn't seem as cool.
I ended up on https://github.com/psake/psake mainly because I wanted to learn Powershell and everyone already had it installed.
Hmm Bounce too https://github.com/refractalize/bounce
You can script your build/deployment tasks with msbuild and then script your tasks and execute them from bat files.

Modifying text files and executing programs with command line parameters in c# or c++ on Linux

I have a need to create a utility in Suze Linux. The utility will make modifications to some text files, and then use the information in those text files to program a device in the computer using a different executable which accepts command line parameters.
I am fluent in c#, but have never worked with Linux. Should I take the time to learn Gnu C++ to do this, or install Mono? How would I execute the programming utility and pass it command line parameters?
Is there a reason you want to restrict yourself to only C++ or C#? There are many options you could consider, for example:
For very simple tasks:
Bash: In some cases a simple Bash script will be able to solve the task. Piping information from one process to another is a breeze, and you have the power of sed, awk, etc. at your fingertips. Another major advantage is that it is installed almost everywhere.
For slightly more involved tasks you could try a scripting language:
Python: Easy to learn, pleasant to read. Very useful for putting together small applications quickly. You can use subprocess to communicate with other processes.
Ruby: Similar comments to Python - a good scripting language with a clean syntax.
Perl: Perl is very good at processing text, although personally I dislike the syntax.
Other options:
Java: Your C# experience will allow you to learn Java quickly as it is a very similar language. Java is officially supported on Linux.
C#: A lot of Linux users are wary of C# because it isn't free enough, but obviously that isn't a worry for you. It has worked fine the few times I've tried it. Note that Mono is not 100% compatible with Microsoft's version.
C++: For what you're planning to do I personally wouldn't recommend C++. It will solve the task though, and if you already know some C++ then I guess it is worth considering.
Obviously there are many other suitable options too.
In C and C++ there's system("command arguments") which you can use to execute a command using the system's interpreter (i.e. you can use it as though you had typed in the command in the shell). The command string can be constructed at runtime. I'm not very familiar with C#, but if I recall correctly you can use Process and ProcessStartInfo classes to run system commands.
Based on the complexity of your program I'd recommend using a scripting language like Perl. It's always a good idea to have a scripting language in your toolbox.

Writing my own custom command line "wrapper" for windows

I have never been a fan of the windows command line. I have tried tools like powercmd and liked them, but most are not distributed for free and I don't relish the thought of paying for something that I think I could write myself. I want to write my own command line wrapper similar to powercmd that allows for these properties:
Custom fonts and colors
Opacity of windows
Multiple windows opened at same time in a panel (maybe like tabbed browsing)
Ability to resize windows
I am reaching out to you guys now to help me decide on whether I should attempt this with C# in visual studio or whether I should do it in Java with Swing. I am comfortable with both. Has anyone ever done a command line wrapper like this? If so what language did you use and what was your experience? Thanks for any feedback.
Grant-
If you want to do a windows command line, I would recommend C#. Java's enforced platform independence will make you fight too much to pass along commands to the underlying OS.
There's already Console2 that hits the big bullet points - resizable, opacity, tabs, modifiable fonts.
It's written in C++ and under the hood it wraps cmd.exe (or whichever command shell you tell it to use) so those may be two strikes against it if you're really interested in developing your own shell in a managed language.
Since you asked, I wrote one myself in C# - the Process class is just too useful. The main thing here is I/O redirection. While I never managed it fully myself, you need this so that subprocess output doesn't appear in another console window. You can also kill programs, find existing ones, etc.
Also, C#'s Console manipulation is very handy.
While I am not a Java programmer, I can imagine that both of those important features would be quite hard to use, considering that Java is platform-independent.
C# has several benefits over Java for this type of project, not the least of which is better integration with Windows, which is (presumably) the only platform you're developing this for. The Java Swing library is not nearly as fine-tuned looking on windows machines as C# forms tend to be, and with the ability to use WPF, C# seems the clear winner to me.
I would not use Java and Swing for this. C# will let you communicate directly with the .Net framework and allow you the ability to build a more powerful command line tool. IT will be a hassle to get Java access to some Windows System calls.
Just wanted to let you all know that I did end up writing a command line wrapper with C#. It turned out really well. I have a couple more little things I want to add and then I will put up the source code as well as a place to download the .exe. I posted a video of it in action in case anyone wanted to provide feedback or ideas. Thanks for the help.
http://www.youtube.com/watch?v=-NM-XcYwLDc

Categories

Resources