Facebook Tweet Google+

RSS

Stop writing batch scripts. Start writing AutoIt scripts.

Jun 2013

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:

  • It can help automate tasks
  • It can run on any Windows computer
  • It does not require any external libraries

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.


About

Dan Nagle is a SW Developer and the founder of NagleCode. His apps have been downloaded hundreds of thousands of times and have been featured all over the internet. He resides in Huntsville, AL.

More Posts

Jan 2018: Pay Down Calc v2.0 - A Resource For Consumers and Agents
Jul 2015: Unity Publishing Metrics
May 2015: JavaScript is not the answer to everything
Feb 2015: Abandoning Scripts for Static Pages
Apr 2014: Qt is better than Java.
Oct 2013: The government does not need to make a health website.
Sep 2013: Google Reader may by dying but RSS is not
Jun 2013: Stop writing batch scripts. Start writing AutoIt scripts.
Mar 2013: Goodbye XP Hello Linux Mint
Dec 2012: Your Goals Should be SMART
Apr 2011: Happy 40th Birthday FTP and Farewell
Aug 2010: Breaking the World Record Typing Numbers to Words

Copyright © NagleCode 2007 - 2018.