DOS Batch - Solid Framework

 

1 Creating a new DOS batch file. 1

2 Editing a DOS batch file. 1

3 Preparing the DOS batch file for the good stuff. 1

4 SETLOCAL ENABLEDELAYEDEXPANSION. 1

5 Adding a Version History. 2

6 Updating the Version History. 2

7 Adding a Window Title. 3

8 Doing something useful. 3

9 Ending the Application. 3

10 Summary and Testing. 3

11 Closing the Application after 5 seconds automatically. 4

 

 

1        Creating a new DOS batch file

Description

Right click in an empty space on your desktop and choose "New - Text Document" from the context menu.

Rename the new created file from "New Text Document.txt" to "dostips.bat"

2        Editing a DOS batch file

Description

Right click on the DOS batch file and chose "Edit" from the context menu.
The DOS batch file will open up in notepad.

3        Preparing the DOS batch file for the good stuff

Description

The command processor needs to be prepared for the good stuff following below. Add the following to the beginning of the DOS file:

Code

@echo off

REM.-- Prepare the Command Processor --

SETLOCAL ENABLEEXTENSIONS

SETLOCAL ENABLEDELAYEDEXPANSION

4        SETLOCAL ENABLEDELAYEDEXPANSION

Description

Adding delayed expansion was a great enhancement for the command processor as it is an enabler for powerful DOS batch coding. Here a simple example that shows the effect. See the also the help for the SET command.

The takeaway is: If you want to process a variable within a block like a loop then turn delayed expansions on.

Test Code

@echo off

SETLOCAL DISABLEDELAYEDEXPANSION

echo.DELAYEDEXPANSION are now DISABLED

set v1=bogus

set v2=bogus

for /l %%a in (1,1,4) do (

set v1=%%a

set v2=%%a

echo.a=%%a, v1=%v1%, v2=!v2!

)

echo Percent sign 1: '%%'

echo Exclamation sign 1: '!'

echo Exclamation sign 2: '^!'

echo.

 

SETLOCAL ENABLEDELAYEDEXPANSION

echo.DELAYEDEXPANSION are now ENABLED

set v1=bogus

set v2=bogus

for /l %%a in (1,1,4) do (

set v1=%%a

set v2=%%a

echo.a=%%a, v1=%v1%, v2=!v2!

)

echo Percent sign 1: '%%'

echo Exclamation sign 2: '^^!'

 

ECHO.&PAUSE&GOTO:EOF

Test Code Output

DELAYEDEXPANSION are now DISABLED

a=1, v1=bogus, v2=!v2!

a=2, v1=bogus, v2=!v2!

a=3, v1=bogus, v2=!v2!

a=4, v1=bogus, v2=!v2!

Percent sign 1: '%'

Exclamation sign 1: '!'

Exclamation sign 2: '!'

 

DELAYEDEXPANSION are now ENABLED

a=1, v1=bogus, v2=1

a=2, v1=bogus, v2=2

a=3, v1=bogus, v2=3

a=4, v1=bogus, v2=4

Percent sign 1: '%'

Exclamation sign 2: '!'

 

Press any key to continue . . .

5        Adding a Version History

Description

Adding and maintaining a version history within you batch file is good practice in order to keep track of when changed what. Add the following code block to you batch file to implement a nicely formatted version history:

Code

REM.-- Version History

REM XX.XXX YYYYMMDD Author Description
SET version=01.000-beta &:20051201 p.h. initial version, providing the framework

REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
SET version=%version: =%

6        Updating the Version History

Description

The version history can be updated by copying the last entry and modifying Date, Author, and Description appropriately, i.e.:

Code

REM.-- Version History

REM XX.XXX YYYYMMDD Author Description
SET version=01.000-beta &:20051201 p.h. initial version, providing the framework
SET version=01.000 &:20051202 p.h. framework ready
SET version=01.001 &:20051203 p.h. added cool new features
REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
SET version=%version: =%

7        Adding a Window Title

Description

Application name and version seems to be just the right title for the application. Using a variable for the title string will help adding additional information to the title later. Append the following code block to the file:

Code

REM.-- Set the title
SET title=%~nx0 - version %version%
TITLE %title%

8        Doing something useful

Description

For now lets show some text to demonstrate the frame work:

Code

REM.-- Do something useful
echo.So far so good.
echo.

9        Ending the Application

Description

Adding a 'pause' at the end will allow the user to inspect screen outputs before closing the application window:

Code

REM.-- End of application
pause

10   Summary and Testing

Description

If you copied all code properly into a batch file then your batch file should look like this:

Code

@echo off

REM.-- Prepare the Command Processor --

SETLOCAL ENABLEEXTENSIONS

SETLOCAL ENABLEDELAYEDEXPANSION

 

REM.-- Version History

REM XX.XXX YYYYMMDD Author Description
SET version=01.000-beta &:20051201 p.h. initial version, providing the framework
SET version=01.000 &:20051202 p.h. framework ready

REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
SET version=%version: =%

 

REM.-- Set the title
SET title=%~nx0 - version %version%
TITLE %title%

 

REM.-- Do something useful
echo.So far so good.
echo.

 

REM.-- End of application
pause

 

This is a good point to run the application. Double click dostips.bat on your desktop.
You should see a command window with the title:
dostips.bat - version 01.000-beta
and the following text in the application window:

Output

So far so good.


Press any key to continue . . .

11   Closing the Application after 5 seconds automatically

Description

As you develop DOS batch files you may find it annoying to have to hit a key when the application finished just to close the window. You may want to have it close automatically but still being able to briefly scan over the execution result shown in the window.

A way to create a delay in a DOS batch program is to ping the localhost or 127.0.0.1. Sending 2 pings (ping -n 2 ...) will cause a delay of very close to one second. Invoking multiple pings as needed gives control about the delay time. Showing the remaining delay time in the window title will even let the user know what's going on.

Below the updated code. Note the changed "-- End of application" section.

When running the application the title will show a countdown of 5 seconds before the application closes. Changing the number 5 to 10 in the FOR /l statement would result in a 10 second countdown. The user can optionally press pause to keep the window open.

Code

@echo off

REM.-- Prepare the Command Processor --

SETLOCAL ENABLEEXTENSIONS

SETLOCAL ENABLEDELAYEDEXPANSION

 

REM.-- Version History

REM XX.XXX YYYYMMDD Author Description
SET version=01.000-beta &:20051201 p.h. initial version, providing the framework
SET version=01.000 &:20051202 p.h. framework ready

REM !! For a new version entry, copy the last entry down and modify Date, Author and Description
SET version=%version: =%

 

REM.-- Set the title
SET title=%~nx0 - version %version%
TITLE %title%

 

REM.-- Do something useful
echo.So far so good.
echo.

 

REM.-- End of application

FOR /l %%a in (5,-1,1) do (TITLE %title% -- closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)

TITLE Press any key to close the application&ECHO.&GOTO:EOF

What it's good for:

·          Closing an application without user interaction

·          Leave enough time to view execution results

·          User can hit Pause on the keyboard to keep the window open.