Batch scripting is a useful tool for Windows software developers, that enables command line instructions to be executed like files. Developers can automate plenty of tasks on servers and on their local machines using batch files. It is especially useful for repetitive tasks, such as setting up new projects or automatically running routine tasks.
Batch files only work on Windows machines, although Mac, Linux, and other UNIX-like machines have similar alternatives. They are a relatively old piece of technology, having originally been developed for DOS and OS/2. As such, they are somewhat uncommon, even among developer circles now.
How it works
Batch files are files that usually end in .bat, but can also end in .cmd or .btm. A batch file is essentially a series of commands in a very high-level language that is read and executed by the operating system. A user can run a batch file by opening it just like they would a .exe file, but instead of opening a program, it will read and run a series of commands contained in the batch file.
Batch scripts are very simple, and commands are executed in sequence. Batch scripts essentially enable you to write and run a series of command prompt operations. Although Windows PowerShell is a more modern alternative that provides more options, batch scripts will still run on contemporary Windows 10 systems, so they can still be useful to developers.
Examples of common batch commands
There is a range of possible uses for batch scripts. From quickly changing the names of all the files within a folder to monitoring all computers connected to a network, batch scripts can do almost everything that can be done from the command prompt. Below are some specific examples of uses for batch files, and what batch files commonly look like.
Virtually every batch file will start with
@echo off on line 1. This is because, by default, Windows will echo (print on the screen) the name of the command being run. This is not often wanted behavior, so adding this on line 1 permanently turns off that feature for the rest of the file.
Common operators like
if, else, and set (to declare variables) are available for use. Special variables that include the name, path of the batch file running, and the current operating system version are also available. Batch scripts can also accept arguments when they run, which are accessible within the script using the convention %1, %2, %3 to refer to each of 3 arguments the file is run with.
call command is also very useful for more complicated scripts.
Call can either run a subprogram within a batch script, or it can be used to run an external batch script. This allows for better code organization and decoupling, allowing for certain best practice programming techniques to be used even when writing Windows batch files. Large scripts can easily get unwieldy, so being able to split them up into discrete functions or files helps a lot with development and readability for programmers.
Goto is another common, though less frequently used, command.
Goto lets you jump to a specific place inside a script. This can be used to run a function or command outside of the usual linear execution path. Depending on how the script is written, this can be useful, though its use has fallen out of favor and is generally considered a 'code smell' by modern programming standards.
Return codes are an important part of batch scripts. By convention, when a function is executed on the command line, it should return zero if the execution was successful and a non-zero value otherwise. These return codes are intended to indicate whether the recently executed command was successful or not.
Fortunately, batch automatically contains the environment variable
%ERRORLEVEL%. This will contain the return code of whatever the most recently executed command was. As a return code of zero means a command was successfully executed, a common way of checking for errors is:
IF %ERRORLEVEL% NEQ 0 ( Execution failed code here )
Screenshot showing the creation of a batch file using the Windows Notepad.
NEQ stands for Not Equal (To), so if the return code was anything other than zero, a programmer can write additional code to handle the error. Unfortunately, return codes are a convention and not an enforced standard, so not all batch scripts may follow this convention.
for command is another useful and common programming tool, which allows you to loop through files and statements to quickly iterate and execute code across a number of files or variables at once. A
for loop contains a condition and code to execute every time that condition is or is not met. A simple
for loop that runs through all the files in a user's profile looks like this:
For %%i in (%USERPROFILE%\*) do Run code here
This can be used to quickly rename lots of files at once, change file extensions, modify file paths, and much more. It is very useful for automating tedious tasks across very many files.
In summary: Batch scripting FAQs
What is batch scripting used for?
Batch scripts are used to execute chains of commands one after another. Batch files can be executed just like .exe files.
What does batch scripting do?
Batch scripting can be used to automate tasks and is particularly useful for repetitive tasks. Most operations that can be executed using the command prompt can also be automated with the help of batch scripts.
Can you run batch files on OS other than windows?
No, batch files are Windows only. Mac, Linux, and Unix-like OS do have similar alternatives though.
Is batch scripting still relevant today?
Yes. While it is a rather old technology, it still has its use cases. There are alternatives like PowerShell, but batch scripts still work on Windows 10.
What are typical batch commands?
- if, else, set