Writing Useful Batch Scripts for Windows


An often overlooked utility in Windows is the use of batch scripts.A batch script is nothing but a text file with the ‘.bat’ extension containing a series of commands to be executed by the Windows command interpreter. Along with calling programs from the command prompt, branching and looping logic is also possible. In this blog, I demonstrate some useful batch script functionality; I use Windows 7 as the operating system.


Windows versions prior to Windows NT were run from MS-DOS and used the same command interpreter, ‘COMMAND.COM’, to execute batch files. Starting from Windows NT, Windows runs directly upon booting the hard drive and is considered a real operating system. Prior Windows versions were just graphical user interfaces applied to the underlying MS-DOS OS. Additionally, a ‘cmd.exe’ was introduced, an enhanced 32-bit command processor that could execute scripts with either the ‘.cmd’ or ‘.bat’ extension.


‘Cmd.exe’ added additional commands, and, unfortunately, implemented existing ones in a slightly different way; in consequence, the same batch file (with a different extension) might work differently with ‘cmd.exe’ (Windows NT and after) and ‘COMMAND.COM’ (prior to Windows NT). In most cases, operation is identical except for a few unsupported commands.


If desired, ‘cmd.exe’ extensions to ‘COMMAND.COM’ can be disabled for compatibility. Microsoft released a version of ‘cmd.exe’ called ‘WIN95CMD’ for Windows 9x and ME to allow older Windows systems to use certain batch files with some ‘cmd.exe’ features. In Windows 8, ‘cmd.exe’ is the default interpreter for batch files; however, the older ‘COMMAND.COM’ interpreter can be run from within a ‘cmd.exe’ window in 32-bit versions of Windows able to run 16-bit programs.


The below batch script file can be double clicked in Windows and the ‘cmd.exe’ interpreter will run the script. The batch script below checks to see if a specific application file is located in the ‘tmp’ directory; if it is, the script will inform the user the application file was detected and will then print the current time and date.


If the application file is not present in the ‘tmp’ directory, the user is informed that the application failed to start. Note how the ‘pause’ command is utilized to prevent the terminal from closing automatically at the end of the script. The user is forced to enter a key prompt to close the terminal window; this ensures the user has had enough time to review the output of the script. Below is the full batch script.

@echo off
@echo:
@echo:
@echo Starting application…
@echo:
if exist “C:\tmp\appStarted.log” (
set Message=”Operation Successful!”
echo Application File Detected
@echo:
echo Time:
time /T
@echo:
echo Date:
date /T
@echo:
) else (
set Message=”Application Failed to Start!”
)
@echo:
@echo %Message%
@echo:
pause


Below is a screenshot of the batch script output if the application file is found in the ‘tmp’ directory.

Batch script output when the application file is found in the 'tmp' directory.

Batch script output when the application file is found in the 'tmp' directory.


Below is a screenshot ot the batch script output if the application file is not found in the ‘tmp’ directory.

Batch script output if the application file is found in the 'tmp' directory

Batch script output if the application file is found in the 'tmp' directory

You can download the full Windows batch script here.

Twitt

Tags: , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

*


nine + 1 =