Batch Scripting

2 04 2006

Did you know what all you could do with DOS Batch files??!! Did you think all you could do was start another batch file, call a few commands to run in sequence!! Well! With DOS and batch commands you can do a lot more, right – you can even read and write to a file, and also it has a somewhat restricted sense of sub-routines in it. So, finally I was introduced to the world of DOS Batch Programming. I learned that Batch processing is a lot more than I would have ever thought it to be. Can you image my surprise when I learned that you can do file manipulations using DOS??? And to add to that, even binary files can be manipulated using the simplest batch commands of DOS?!! Ever heard of the command FOR in DOS! Well! If you add the /F switch with the same, you get a wonderful file handling capabilities for DOS. Read up more on the posts by Mic, an avid and resourceful DOS Batch person, who I think would be able to answer all DOS Batch related questions you post to Batch World group on Yahoo!: Batch World

Well! I have had a fine time trying to work out what all I could to with DOS to help in my Automation work; as currently what I have created is a PXE server which can boot a machine into DOS, from here I have to carry on and cal the Acronis Command Line tool to accomplish my work, but all has to be done using DOS commands only as the environment into which the system is booted into is PURE MS-DOS with a few network enhancements. I now had to work out a way by which I can get my system to work by calling the Acronis Command Line tool and install an image for the system which will be Windows platform and configuration as selected by the user, as this is a dynamic thing, I have had to do a lot of work to create dynamic fiels which would do my work and heer I learned about DOS and Batch File commands. Currently, I have managed to get a working script which will help me in installing images stored by me on a server and a user configuration in which user selects his choice of OS file system to install and execute his test framework on:
FOR /F %%y IN (c:\OSCount) DO CALL :READ %%y
SET /A countWr=%my_return% 1
ECHO %my_return%
> c:\OSCount echo %countWr%
ECHO %countWr%

SET /A v_sum=%1
SET /A count=1
FOR /F %%x IN (c:\MyFileA.txt) DO CALL :ABC %%x
ENDLOCAL & SET /A my_return=%v_sum%

IF %count%==%v_sum% ECHO This File %1
ENDLOCAL & SET /A count =1


The above script is reading a file (MyFileA.txt) and getting the line number to be read from another (OSCount) file, which is then incremented for the next execution (the file is written to, we can also append to the file by using double greater than symbol – >>). Also, the variables are being passed as parameters from one sub-routine to another and then returned. So, finally, when we reach the first sub-routine again, the OSCount file is contains an incremented new value and when we run it again, we can get the next line in the file MyFileA.txt.

Although with the above script you can find out many things you can do with DOS batch commands – read a particular line in a file, write or append a line to a file, and even sub-routines in DOS!!! I never knew these were there. The SETLOCAL – ENDLOCAL actually defines a sub-routine in DOS. For debugging DOS batch files, you just do an @ECHO ON at the beginning of the file and execute the script through the DOS command line and viola, you can actually pin-point where you are going wrong – which line or variable is returning what value and where and when.

So, folks all I can say is that get your Windows machines pumped up and start using the powerful features of DOS which you might have never know existed… (PS: I have not added the code sent by Mic here as the link should take you to that, which is more nicely structured and concise!! My code is just a bunch of statements more meant to give an idea of what all DOS is capable of doing…)




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: