|Facebook Share||Tweet This|
Every time I see a DOS .bat file that is more than 20 lines, I get an immediate urge to rewrite it in a better language.
As far as I can see, there are just a handful of non-trivial benefits to writing a DOS batch script:
That's actually about it. There are lots of languages that can automate tasks, but they all require you to install something to be able to run it. A bat file can run on any Windows computer, and it is built-in. No installer needed. This is why people really like it.
However, that is where the benefits stop. Anything more than 20 lines, batch scripting gets very complicated and very difficult to debug. You need a real language. Here is a "complex" example I grabbed from Wikipedia:
@echo off color 3 title Conditional Shutdown set /p name=enter a name: :start cls echo Hi, %name% echo. echo 1.Shutdown echo 2.Quit set /p choice=enter your choice 1,2: if %choice%==1 goto shutdown if %choice%==2 exit :shutdown cls set /p sec=enter the number of seconds that you wish the computer to shutdown in: set /p msg=enter the shutdown message you wish to display:
A simple shutdown script. Anything beyond this, I would start writing an AutoIt file. Here is my AutoIt version following the same logic flow as the original.
#comments-start This code is MIT licensed by Dan Nagle #comments-end #include Opt('MustDeclareVars', 1) Dim $name = InputBox("Conditional Shutdown", "enter a name:") If $name == "" Then Exit EndIf Dim $msg Dim $guiHandle = GUICreate("Hi, " & $name, 200, 60) Dim $blurbLabel = GUICtrlCreateLabel("enter your choice", 5, 5) Dim $shutdownbutton = GUICtrlCreateButton("Shutdown", 5, 30, 75) Dim $exitbutton = GUICtrlCreateButton("Exit", 120, 30, 75) GUISetState(@SW_SHOW, $guiHandle ) While 1 $msg = GUIGetMsg($guiHandle ) Select Case $msg == $GUI_EVENT_CLOSE Or $msg = $exitbutton Exit Case $msg == $shutdownbutton Dim $seconds = InputBox("Number of Seconds", "Enter the number of seconds that you wish the computer to shutdown.") If $seconds == "" Then ContinueLoop EndIf Dim $shutdownMsg = InputBox("Shutdown message", "Enter the shutdown message you wish to display.") If $shutdownMsg == "" Then ContinueLoop EndIf ;do shutdown RunWait(@ComSpec & " /c " & "shutdown -s -f -t " & $seconds &" -c """& $shutdownMsg &"""") Case Default EndSelect WEnd
The AutoIt version is a little more verbose, but I find it easier to read. It is also much more powerful. It is a real scripting language with scope, functions, etc. It can do some complicated things and keep you organized. Plus, unlike batch, it has a GUI library.
Just like batch files, AutoIt can run on any Windows computer without any external libraries. It compiles to a single executable. And it too is free. The only real trade-off is you cannot just simply open it up and edit it like a normal batch file, but some automation users concerned about security might consider that a positive.
So my advice is: Stop the fancy batch scripting and start the simple AutoIt scripting. You'll be happier 3 years from now when you try to debug it.