Migrating a batch CMD file to AutoIt (Simple)

A batch process is a series of commands or executable processes from the command-line CMD (DOS shell) and is typically stored in a .bat extension plain text file (there are more extensions and more shell script languages that may be logically imported). This is widely used by Windows system administrators because it allows the automation of many maintenance tasks, such as performing backups, searching for files, renaming multiple files in bulk, user management and permissions, user feedback, and so on.

Using AutoIt, you can extend or replace these BAT files, improving them in many ways, and add unexpected new features, for example, getting a graphical mode with a nice user interface (GUI) for a .bat file or a corporate identity. You can run uncompiled .au3 files (same as batch files), so it is a nice and extended replacement for the previous .bat approach, improving the experience and adding friendly graphical interfaces. Later, you can convert (compile) these source code text files into executables.

Getting ready

Open SciTE from the AutoIt Program Group and start writing code.

Open your batch file in Notepad or your favorite text processor to copy and paste the following codes into SciTE.

How to do it...

We have to translate the DOS CMD sources into AutoIt code:

  1. The following lines show the DOS batch source code script (see the How it works section for details on this):
    echo off
    cls
    cd c: 
    :checkdir
    if "backup" == "" goto createdir
    copy c:  data  *. * c:  backup  / s
    echo "Successful Backup "
    pause
    goto end
    : createdir
    md c:  backup
    goto: checkdir
    : End
  2. Now, we will translate the previous CMD batch logic to an AutoIt script:
    ; "folder does not exist" If/then flow control check. 
    If Not FileExists ("C:BACKUP") Then 
    RunWait (@COMSPEC & "MD C:BACKUP") 
    ; "backup" folder created. Autoit native function: DirCreate("path"). 
    EndIf 
    RunWait (@COMSPEC & "COPY C:DATA*.* C:BACKUP / s") ; Run wait till copy ends 
    ; you may also use Autoit native function: Filecopy("source","dest") 
    MsgBox (0, "Process finished", "Copy Successful") 

    You can improve this script in many ways, for example, by checking the integrity of copied files, by asking users if they want files to be overwritten, and so on.

How it works...

A batch template and examples create a Backup folder. It will copy the source folder's data files, thereby overwriting files in the destination backup folder called Backup.

The easiest way to convert a BAT file to AutoIt is by using the RunWait() function. It executes the command parameter and continues with the next script instruction, waiting until it has completed the previous process. You may also use Run() (it does not wait for the application to terminate), so that you can create asynchronous tasks.

The @COMSPEC parameter tells AutoIt where the right location of the operating system's DOS shell is.

You may also replace DOS commands with AutoIt's native functions or external apps in the batch command mode.

There's more...

By comparing both the codes, it is evident that the same process is syntactically more readable with AutoIt, which has fewer lines of code and an enhanced CMD batch, and has the ability to add comments to your code to review it later in a more easy and effective way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset