MOBZcript

MOBZcript is an extremely simple script editor for cmd batch files. One of the problems with writing and testing batch files is that behaviour from the command line is subtly different from 'interactive' mode, i.e. you have to double percent-signs, there's no SETLOCAL, etc. Plus, you can't split statements over several lines when in interactive mode, which is one really convenient features of batch files.

What I need is a way to type a small script and then have it executed on the command line. I want to pass arguments, and I want the output to pause, even when the batch file ends unexpectedly.

VoilĂ : MOBZcript. It looks like this:

and basically, what you see is what you get. The File menu offers Open, Save, Save As, and Exit; the Script menu contains a single item: Run (F5). It's meant to start up fast, so when you run it, it'll create a file called 'untitled-1.cmd' in your temp-directory and run from there. You can, of course, save your script under a different name in a different location, and it'll run from there. Note: there's no New menu item - I forgot that. I'll add it in the next update.

As you can see from the source code, there's no magic involved - not even rocket science. On Run (or F5, of course) MOBZcript saves the script you're editing in its intended location, and then saves a wrapper script in your TEMP-directory. This wrapper script changes the current drive and directory to your script location, enables or disables ECHO depending on the checkbox setting in the toolbar, and CALLs your script using the arguments supplied in the toolbar. That's it. The only difficult part was how to construct a working command line, so that the wrapper script gets called and a PAUSE statement is executed regardless of the outcome of the script, or whether it contains errors.

The result looks like this:

As you can see, a PAUSE statement was executed (albeit in Dutch) and the result of the script - in this case 12 - is shown.

The heavy lifting is done by the handler of the Run menu item:

Dim tempScript As  String = String.Format(
  "@echo --- Starting script..." + Environment.NewLine +
  "@echo {2}" + Environment.NewLine +
  "{4}" + Environment.NewLine +
  "@cd {3}" + Environment.NewLine +
  "@call ""{0}"" {1}" + Environment.NewLine +
  "@echo --- Script returned %ERRORLEVEL%" + Environment.NewLine,
  Me._filename,
  Me.argumentsTextBox.Text,
  If(Me.echoCheckButton.Checked, "on", "off"),
  Path.GetDirectoryName(Me._filename),
  driveName
)

File.WriteAllText(tempPathName, tempScript)

Dim args As String = String.Format("/c (cmd /c ""{0}"")&pause", tempPathName)
Dim p As Process = Process.Start("cmd.exe", args)

When your script is named c:\src\Project1\SampleScript.cmd, and your arguments are "one two three" this produces a script of the form:

@echo --- Starting script...
@echo off
C:
@cd C:\src\Project1
@call "C:\src\Project1\SampleScript.cmd" one two three
@echo --- Script returned %ERRORLEVEL%

This scipt is saved in your TEMP-folder and then started using the command line

cmd.exe /c (cmd /c "%temp%\_MOBZcript.cmd")&pause

Basically, the first cmd starts the command line (run the script)&pause, which runs the script and then executes a pause-statement regardless of the outcome of the script. (run the script) is in its turn a call to cmd /c with the name of the script.

Well, there you have it. All in all, MOBZcript turns out to be a remarkably useful little tool, considering the amount of code involved. I think, anyway. And I hope you'lll like it too.

Enjoy!

Download MOBZcript

Download MOBZcript Download MOBZcript 0.9.0 (December 10, 2012) from CodePlex or browse source code

Simple 32-bit/64-bit executable. Download and run. Requires .NET v4.

Comments? Bugs? Suggestions? Feature requests? Let us know! Alternatively, you can create a work item on Codeplex.