Coding in MS-DOS Batch

http://www.trytoprogram.com/batch-file-commands/

History

Batch has existed since almost the introduction of computers to the commercial market. The first computer to ever use Batch was the IBM 5150. For the first four years of its existence, it dominated the computer market until more complex methods of displaying information, like GUI’s, became mainstream with the introduction of the Apple Macintosh. Since then, the need and use to use Batch and the command line, in general, have declined from something that everyone should know to a niche hobby or odd programmer.

Pros and Cons of Batch

Batch is an elementary language to learn. Thanks to Windows’s devotion to backward compatibility, it is very likely to survive into the future, even if it gets less effective as time continues. On the other hand, Batch is quite a weak language without the assistance of its much stronger cousin Powershell. It has fundamental file reading and writing capabilities, although its integration into the language from the get-go is somewhat functional. It is relatively limited in its graphics capabilities, with the only options being ASCII characters printed left to right, top to bottom. However, this does give it a nice retro aesthetic if you program video games in it.

Who uses it?

At this point, Batch is a very niche coding language with a small community of less than 3500 on Reddit (at https://www.reddit.com/r/Batch/). It would be fair to assume that the number of people who actively code in it is less than 100,000. Due to it being relatively obsolete and low level, it would be fair to assume that less than 10,000 people actively use it. It is predominantly used to deal with files but can also be used to create games, which have been posted to the subreddit linked above.

Structure of Batch

Batch is a data-oriented coding language with a fundamental and easy-to-learn design. The most commonly used Batch commands and syntax can be found by opening the command prompt and typing in “help.” It is easy to learn most of Batch as you generally only use a couple of different commands repeatedly while programming, and the rest are easy to understand and access.

Differences from other coding languages

Batch only has basic integer and string type variables and only the primary four math operations (+, -, *, and /). To make it even harder to program, its variant of functions (the “Call” command) can only set variables already defined. Batch also has not every coding option enabled by default, and in particular, Batch’s version of pointers requires a “setlocal” command to be called first.

Quirks you should know and how to work around them

Extra text outputs

If you run a .bat file, every command is pasted into the command prompt unless you add @echo off to the top. If you only want a specific line not to show up, put an @ in front of it.

Loops

Batch only has a built-in “for” loop, but it is possible to make a “while” loop by using an IF statement to execute a GOTO statement, as seen below:

set /A condition=10:whileloopstartset /A condition=%condition%-1if NOT (condition==0) goto :whileloopstartecho %condition%This code would put 0 onto the screen before terminating.In Batch, FOR loops also focus on parsing through files, so you have to use the /L modifier to work with numbers. See below:"For" loop example:for /L %%I in (1,2,5) do (echo %%I)

Folders and directories

One of Batch’s primary purposes was to loop through files and change directories. And because of this, it has several commands dedicated to doing this, such as the “dir”, “cd”, “chdir” and many, many more. I would recommend opening up a command prompt and typing “help” to see most of them, but I will try to explain the basics here.

Variable modificationWhat it evaluates to%~IExpands %I and removes surrounding quotes%~fIExpands %I to a pathname%~dIGets the drive letter of %I%~pIGets the path of a file in %I%~nIGets the file name of %I%~xIGets the file extension of %I%~sIExpands to a path containing shortened names%~aIGets the attributes of the file in %I%~tIGets the time and date of %I%~zIGets the size of the file of %I%~$PATH:ISearches the %path% variable for the %I file

Arrays

the only way to use an array in Batch is to create a separate variable for each array element and then use delayed expansion to call to it. An example of making an array is below:

for /L %%Χ in (1,1,5) DO (for /L %%Y in (1,1,3) DO (set "arrayA%%Xx%%Y=0"))
setlocal enabledelayedexpansionset indexX=2set indexY=1!arrayA%indexX%x%indexY%!
setlocal enabledelayedexpansion!arrayA%%Xx%%Y!

Functions

The closest thing Batch has to functions is the “CALL” command that passes (or inputs) in some parameters, jumps to the ‘function’ definition, executes the function, and returns to where it was called. Use an “exit” before the function declarations; otherwise, Batch will run the function after the code.

set “printvalue1=Im just going to print this onto the screen”set “printvalue2=and a second line to show the difference”call :printToScreen %printvalue1% %printvalue2%exit:printToScreenecho %1exit /b

User Input

There are two ways to get user input in Batch, and both are text-based. You can either use the “set” command with the prompt modifier (/P) or the “choice” command. Examples for both of these are below:

set /P "userinput=whatever you want to prompt the user goes here."echo %userinput%
choice /C ABC /N /M "prompt goes here."if %ERRORLEVEL%==1 echo you typed Aif %ERRORLEVEL%==2 echo you typed Bif %ERRORLEVEL%==3 echo you typed C

Piping

Piping is a feature that I believe is unique to Batch, it’s not very essential, but it is useful when creating files. Most of the time, piping is used to get input from a file or to put the output to a file, but it also includes chaining functions. Piping has four different types (listed here), with each type being associated with a symbol.

Vertical bar |

The vertical bar piping symbol means “take the output of whatever comes on the left, and apply the function on the right.” some particular uses of this are sorting the output (via command | sort), copying it to the clipboard (via command | clip).

Greater than symbol >

The greater than symbol acts similarly to the vertical bar in that it directs the output to somewhere else. Generally, it is used to output to a file (i.e., echo “This is going to be in a file”> text.txt). You can also make the distinction between error messages and normal output by using 1>file to redirect normal output and 2>file to redirect any errors that occur while executing that line.

Less than symbol <

The less than symbol is used to input a file or command into another file or command. It is slightly different from the greater than symbol in that it acts in reverse-that is to say that you reverse the order of reading from right to left, to the left to right- and can be used to get all text from a file at the same time. Still, otherwise, its usage is identical to the greater than a symbol.

Double greater than symbol >>

Suppose you use 2 greater than symbols instead of one when piping to a file. It appends to the end of the file instead of overriding the entire file. It can also be used in conjunction with the 1>> and 2>> to append the output and error messages to the file. There is also a slight variation on the double greater than symbol, the greater than ampersand 2>&1, which is used to direct both errors and the standard output through the same text stream.

Takeaway

In general, Batch is a fascinating language with its hurdles and obstacles to overcome. I hope that this article provides an excellent introduction to some of the complexities of Batch, even though this is in no way an exhaustive explanation of Batch.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store