Coding in MS-DOS Batch
Batch is a coding language built into windows computers. When executed, batch programs are saved with the .bat extension and run in command prompt.
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.
Batch cannot also interact over the internet, which makes it reasonably weak nowadays as a lot of the time, coding languages are expected to work on the internet. There is also a significant lack of online emulators due to Batch’s origins of dealing with files instead of software production.
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.
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)
Would output 1 3 5 each on separate lines before terminating.
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.
The “dir” command shows everything in the current directory (or folder), and the cd and chdir allow the user to navigate through directories (again, folders) to access whatever one they want.
The “tree” command also gives the user a graphical representation of the directories and file structure centered on the current directory.
“Chkdsk” lets the user switch from one disk to another if the user has multiple installed on the one computer.
If the user wants to add a new folder or directory, they can use the “md” or “mkdir” command followed by the folder name if in the current directory or the path in which the folder will appear not in the current directory.
For loops may be used to traverse through files and execute code on each file or directory in the specified section. When used in this way, you would type “for /D %file in (fileset) do” to take each file in the fileset and execute the code after “do” with %file referring to the file in the fileset.’
You can also use for /R to execute the code in all subdirectories of the set of files or directories provided. You can give additional settings with the /F switch to hone your search better. When used with the /F switch, there are several extra variables available to the user, if %I is the default variable used, then replacing % with %~ followed by a modifier can give you the current path, drive, file, and so on. There is a complete list below:
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
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"))
Which would generate an array that looks like this:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
Each item can be accessed by:
setlocal enabledelayedexpansionset indexX=2set indexY=1!arrayA%indexX%x%indexY%!
Where indexX and indexY can be any value, you want. You also can use %%X and %%Y if you are looping through all the elements like so:
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.
NOTE: If you use the call command as a function, the section you call has to end with “exit /b”; otherwise, it keeps running until the end of the file or until the next exit command.
An example of how to setup functions is below:
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
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%
This returns what the user typed in. If it is completely an integer, then it is automatically converted to a number rather than a string.
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
After the /C you list the possible inputs, A, B, and C. The user then types one of the options, and %ERRORLEVEL% is set to what position the input was in the list. You can use /D and /T to specify a timeout in seconds before selecting the default option like so:
choice /C ABCD /N /T 10 /D D /M “you have 10 seconds, A, B, C, or D?”
After 10 seconds, it will automatically act as if the user imputed D. By default, and only A-Z can be used as inputs. If you want to go past that, you need to chain several choice commands together. Still, if you use a /CS, you can differentiate between capital and lowercase letters to get 52 possible values from the choice command.
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.
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.