MS/PC DOS Batch File Programming
using AWK

In July/August of 1997 an associate contacted me in reference to coding some batch files for one of his clients. I have cleaned up and removed our clients specific information and offer the following as tips or ideas. I am not writing, nor is this article intended to be, an exhaustive tutorial on batch file programming or AWK programming. Again, the following information is presented only as examples or ideas that you can use.

In the following sections we will show embedding AWK source code in MS/PC DOS (here after called DOS) batch (BAT) files.

First we will show some simple single line embedded AWK code. The first 'bat' file is 'ASK.BAT'. ASK.BAT prompts the user for some information and stores it in an environment variable. The Second example is 'RIGHT.BAT' which will set an environment variable (evar) with the right most 'N' characters. The third example is 'LOOP01.BAT'. It demonstrates a batch file looping construct calling an external/additional batch file to set an evar. The forth example is a derivation of LOOP01.BAT it set the evar from within itself 'LOOP02.BAT'.

Note the comments are in GREEN, DOS code in BLACK, AWK code in MAROON.

In addition to the batch files that contain AWK Code I have included a batch file which sets an environment variable equal to the value of the ERRORLEVEL (Return Code). That Batch file is 'ERR2VAR.BAT'.

Example: ASK.BAT
Prompt the User for some information

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@echo off

REM Pgm : ASK.bat
REM Author : L.E. Drake
REM E-Mail : ldrake@bigfoot.com
REM Date : 09/13/97
REM Purpose: Demo use of AWK and batch file programming.
REM This program asks the user for some information (input)
REM and stores the response in an ENVIRONMENT variable.

cls

REM Note the following statement will print the prompt and leave the cursor on the same line!!

awk "BEGIN{printf(\"Enter Name: \")}"

REM Get data from the user
awk "BEGIN{getline < \"-\";printf(\"set name=%%s\n\", $0)}" > out.bat
REM Check to be sure the file was created! There are cases when
REM the file is not created.

if exist out.bat call out.bat
if exist out.bat del out.bat

REM Show the values
set | more

:end

Arrow leftArrow right

Example: RIGHT.BAT
Save the Right most 'N' characters from a string.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@echo off

REM Pgm : RIGHT.bat
REM Author : L.E. Drake
REM E-Mail : ldrake@bigfoot.com
REM Date : 09/13/97
REM Purpose: Demo use of AWK and batch file programming. This
REM program gets the right most n chars in a string
REM and stores the response in an ENVIRONMENT variable.

cls

set VAL=1234567890
echo %VAL%| awk "{printf(\"set rt=%%s\n\", substr($0,length($0)+1-3,3))}" > rv.bat
if exist rv.bat call rv.bat
if exist rv.bat del rv.bat

REM Expect "RT=890"

REM Note the diff in the values of rt & rt1. The space between val and the
REM pipe is significant FYI!!!


echo %VAL% | awk "{printf(\"set rt1=%%s\n\", substr($0, length($0)+1-3, 3))}" > rv.bat
if exist rv.bat call rv.bat
if exist rv.bat del rv.bat

REM Expect "RT1=90 "

set | more

REM Clean Up
set VAL=
set RT=
set rt1=

:end

Arrow leftArrow right

Example: LOOP01.BAT
Demonstrate Fixed interval loop with a call to an external 'BAT'.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@echo off

REM Pgm : LOOP01.bat
REM Author : L.E. Drake
REM E-Mail : ldrake@bigfoot.com
REM Date : 09/13/97
REM Purpose: Demo use of AWK and batch file programming. This
REM program demonstrates using awk to increment a counter
REM The program calls ERR2VAR.bat to actually set a E-Var
REM 'E'.

cls

REM Init CNT - Counter Var to -1 so that the first errorlevel is zero.
REM Init LM - Loop Max
set CNT=-1
set LM=5

:loop

REM Increment Exit Code Number (ERRORLEVEL)
awk "BEGIN{exit(%CNT%+1)}"
REM ERR2VAR sets - Environment Var 'E' equal to the Errorlevel
call err2var.bat
set CNT=%E%
if errorlevel %LM% goto done
echo CNT is %CNT%
goto loop

:done

set CNT=
set LM=
set E=

Arrow leftArrow right

Example: LOOP02.BAT
Demonstrate Fixed interval loop 'BAT'.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@echo off

REM Pgm : LOOP02.bat
REM Author : L.E. Drake
REM E-Mail : ldrake@bigfoot.com
REM Date : 09/13/97
REM Purpose: Demo use of AWK and batch file programming. This
REM program demonstrates using awk to increment a counter
REM The program caretes it's own 'bat' file to set a E-Var.

cls

REM Init CNT - Counter Var to -1 so that the first errorlevel is zero.
REM Init LM - Loop Max

set CNT=-1
set LM=5

:loop

awk "BEGIN{printf(\"set CNT=%%03d\",%CNT%+1);exit(%CNT%+1)}" >doit.bat
if errorlevel %LM% goto done
if exist doit.bat call doit.bat
if exist doit.bat del doit.bat
REM -----------
REM Do processing here
REM -----------

echo CNT is %CNT%
goto loop

:done

set CNT=
set LM=

Arrow leftArrow right

Example: ERR2VAR.BAT
Store the Value of ERRORLEVEL in an Environment Variable.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@ECHO OFF
REM Pgm : ERR2VAR.bat
REM Author : L.E. Drake
REM E-Mail : ldrake@bigfoot.com
REM Date : 09/13/97
REM Purpose: Set the ERRORLEVEL (return code) set by another
REM program (IE a HILL) to an "E-VAR" (ENVIRONMENT VARIABLE)
REM
REM At the conclusion of this program the E-Var 'E' is set to
REM the value of the current return code (ERRORLEVEL).
REM
REM Historical note: In the early days of DOS some time between DOS
REM ver 2.0 and ver 3.0. I created a batch program by the same name.
REM My program went something like this:
REM echo off
REM if errorlevel 255 set e=255
REM if errorlevel 255 goto done
REM if errorlevel 254 set e=254
REM if errorlevel 254 goto done
REM *
REM *
REM *
REM if errorlevel 2 set e=002
REM if errorlevel 2 goto done
REM if errorlevel 1 set e=001
REM if errorlevel 1 goto done
REM if errorlevel 0 set e=000
REM :done
REM
REM The program worked but was a 'hog' on disk space which was a premium at
REM the time. The following version was/is smaller and faster than my
REM original. You can recreate a pgm like my original by running 'ev.awk'
REM which will create 'ev.bat' The original author of the following version
REM of the batch file is unknown to me. In any event, enjoy! - Laurence
REM

REM Clear E-Var 'E'

SET E=

FOR %%q IN (0 1 2) DO IF ERRORLEVEL %%q00 SET E=%%q

IF "%E%"=="2" GOTO over200

FOR %%q IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %E%%%q0 SET E=%E%%%q

GOTO digit2

:over200

FOR %%q IN (0 1 2 3 4 5) DO IF ERRORLEVEL %E%%%q0 SET E=%E%%%q

GOTO digit3

:digit2

IF "%E%"=="25" GOTO over250

GOTO digit3

:over250

FOR %%q IN (0 1 2 3 4 5) DO IF ERRORLEVEL %E%%%q SET E=%E%%%q

GOTO done

:digit3

FOR %%q IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %E%%%q SET E=%E%%%q

:done

Arrow leftArrow right

Example: EV.AWK
Create ERR2VAR.BAT (the old way)

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#++
# Pgm : EV.AWK
# Author : L.E. Drake
# E-Mail : ldrake@bigfoot.com
# Date : 09/13/97
# Purpose: Create EV.BAT
#--
BEGIN{
OutFile = "ev.bat"
printf("echo off\n") > OutFile
for (i = 255; i >= 0; i--) {
printf(" if errorlevel %03d set E=%03d\n",i,i) >> OutFile
printf(" if errorlevel %03d goto done\n",i,i) >> OutFile
} # End For

printf("\n:done\n") > OutFile
} # End BEGIN


 

$Revision: 1.1 $

Return to my Home Page

E-Mail

This page updated on Thursday 03/05/1998


This page hosted by Get your own Free Home Page
1