views:

63

answers:

1

I need to do a fair bit of scripting in my job as a SQL Server DBA. Sometimes, I need to deploy a fix script to a very restricted environment, where the only option for scripting may be DOS Batch. In one such environment, even VBScript/WSH isn't a possibility, let alone PowerShell. Anyone who has written enough batch files on DOS and Windows knows that it's very limited and a huge PIA when you need to do anything too complicated. This is especially true for folks who have worked with Unix shell scripting, Perl, Tcl, Python, Ruby, etc.

A possible solution to this would be a CMD preprocessor that would add some of the useful functionality from more capable scripting languages. I've tried to find such a utility, but so far I've had no luck.

Which finally leads to my question: is anyone aware of a such a CMD preprocessor? If not, what functionality would you like to see in one?


Addendum:
If you're unfamiliar with the idea of a preprocessor see this Wikipedia entry.

To clarify, I'm thinking of a tool that would add features like:

  1. Functions
  2. Backtick (`) ala Unix shell

...and possibly others. Those are two features I've wished CMD had and can think of a way to implement them with a CMD preprocessor. Functions could be implemented with env vars and GOTO/labels; backticks by piping to a temp file and using set /p =< to read in the result to an env var.

You can already achieve these same ends, but it gets to be very tedious and verbose- which is how I came to the idea of having a preprocessor handle the boilerplate for features like those.

Example

Using the example of backticks, here is an example of unprocessed code from my hypothetical Batch++ and processed vanilla batch script, ready to be run by CMD.exe:

Batch++ Source (test.batpp)

copy `dir /b /s c:\ | find "CADR-README.htm"` \\srv01\users

Run it through the preprocessor

bpp test.batpp > post_test.bat

Resulting CMD/BAT code (post_test.bat)

dir /b /s c:\ | find "CADR-README.htm" > _bt001.tmp
set /p _BT001 =< _bt001.tmp
copy %_BT001% \\srv01\users
set _BT001=
del _bt001.tmp
A: 

I am not sure to interpret correctly your question. If you run in a controlled environment that doesn't allow you to run any scripting extension, how are you going to access such a preprocessor?

However, and in regard of the two features you request, you are OK with .BATs. Both features are supported by BAT processing in current Windows versions.

  1. Functions: you have the extended CALL syntax, that supports parameter passing thru argument references %1 .. %9, and enhanced with expansion substitution using %~ syntax. Read HELP CALL.

  2. Backtick: not sure what you want but, in the FOR /F command you may pass a backticked string to be run and its output captured. Read HELP FOR.

PA
If you're unfamiliar with the idea of a preprocessor see this Wikipedia entry: http://en.wikipedia.org/wiki/PreprocessorThe restrictive deployment env is precisely why a preprocessor came to mind. On my own machine, I could write scripts in this CMD++, which incl. new functionality and syntax sugar. For deployment, I'd use it like any other preprocessor: run the CMD++ source through the preprocessor, which outputs vanilla CMD code.For a description of what the backtick does, see: http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch05s04.html
Aaron
Some of the preprocessors I know result in .EXE code, which you will not be able to run neither. Not one that results in .BAT that I know. Hope someone here at SO can help. Regarding backtick, I believe that FOR /F does exactly what you need. Try `FOR /F %A IN ('date /t') DO ECHO %A'`
PA