A Windows batch file is simply a script file that runs a series of commands, one line at a time, much in the same fashion as a Linux script. The series of commands are executed by the command line interpreter and stored in a plain text file with the .BAT extension; this signifies to Windows that it’s an executable file, in this case, a script.
Batch files have been around since the earliest versions of Microsoft DOS. Although not exclusively a Microsoft scripting file, batch files are mainly associated with Microsoft’s operating systems. In the early days, when a PC booted into a version of DOS (which produced a simple command prompt when powered up), the batch file was used in the form of a system file called Autoexec.bat. Autoexec. bat was a script that automatically executed (hence Autoexec) commands once the operating system had finished dealing with the Config.sys file.
When a user powered up their DOS-based computer, and once the BIOS had finished checking the system memory and so on, DOS would look to the Config.sys file to load any specific display requirements and hardware drivers, allocate them a slot in the available memory, assign any memory managers and tell the system where the Command.com file, which is the command line interpreter for DOS, was. Once it had done that, then the Autoexec.bat file took over and ran through each line in turn, loading programs that would activate the mouse or optical drive into the memory areas assigned by the Config.sys file.
The DOS user of the day could opt to create different Autoexec. bat files depending on what they wanted to do. For example, if they wanted to play a game and have as much memory available as possible, they’d create a Config.sys and Autoexec.bat set of files that loaded the bare minimum of drivers and so on. If they needed access to the network, an Autoexec.bat file could be created to load the network card driver and automatically gain access to the network. Each of these unique setups would be loaded on to a floppy disk and booted as and when required by the user.
The Autoexec.bat was the first such file many users came across in their PC-based computing lives; since many had come from a 16-bit or even 8-bit background; remember, this was the late eighties and early nineties. The batch file was the user’s primary tool for automating tasks, creating shortcuts and adventure games and translating complex processes into something far simpler.
Nowadays however, a batch file isn’t just for loading in drivers and such when the PC boots. You can use a batch file in the same way as any other scripting language file, in that you can program it to ask for user input and display the results on the screen; or save to a file and even send it to a locally or network attached printer. You can create scripts to back up your files to various locations, compare date stamps and only back up the most recently changed content as well as program the script to do all this automatically. Batch files are remarkably powerful and despite them not being as commonly used as they were during the older days of DOS, they are still there and can be utilised even in the latest version of Windows 10; and can be as complex or simple as you want them to be.
So what do you need to start batch file programming in Windows? Well, as long as you have Windows 10, or any older version of Windows for that matter, you can start batch file programming immediately. All you need is to be able to open Notepad and get to the command prompt of Windows.
Batch File Power
Just like any other programming interface that can directly interrogate and manipulate the system, batch files require a certain amount of care when programming. It’s hard to damage your system with a batch file, as the more important elements of the modern Windows system are protected by the User Account Control (UAC) security; UAC works by only allowing elevated privileges access to important system files. Therefore if you create a batch file that somehow deletes a system file, the UAC activates and stop the process.
However, if you’re working in the command prompt with elevated privileges to begin with, as the Administrator, then the UAC won’t question the batch file and continue regardless of what files are being deleted.
That said, you’re not likely to create a batch file that intentionally wipes out your operating system. There are system controls in place to help prevent that; but it’s worth mentioning as there are batch files available on the Internet that contain malicious code designed to create problems. Much like a virus, a rogue batch file (when executed with Administrator privileges) can cause much mayhem and system damage. In short, don’t randomly execute any batch file downloaded from the Internet as an Administrator, without first reviewing what it does.
You can learn more about batch files in the coming pages, so don’t worry too much about destroying your system with one. All this just demonstrates how powerful the humble batch file can be.
Get Started With Batch Files
Before you begin to program with batch files, there are a few things you need to know. A batch file can only be executed once it has the .bat extension and editing one with Notepad isn’t always straightforward.
Step 1 – The Windows command prompt may look a little daunting to the newcomer but it’s simply another interface (or Shell) used to access the filesystem. You can go anywhere you like in the command prompt, as you would with the graphical interface. To begin, click on the Windows Start button and enter CMD into the search box.
Step 2 – Click on the search result labelled Command Prompt (Desktop App) and a new window pops up. The Command Prompt window isn’t much to look at to begin with but you can see the Microsoft Windows version number and copyright information followed by the prompt itself. The prompt details the current directory or folder you’re in, together with your username.
Step 3 – While at the command prompt window, enter: dir/w. This lists all the files and directories from where you are at the moment in the system. In this case, that’s your Home directory that Windows assigns every user that logs in. You can navigate by using the CD command (Change Directory). Try:
Then press Return.
Step 4 – The prompt should change and display \ Documents>; this means you’re in the Documents directory. Now, create a new directory call Batch Files. Enter:
md “Batch Files”
You need the quotations because without them, Windows creates two directories: Batch and Files. Now change directory into the newly created Batch Files.
cd Batch Files
You won’t need the quotes to change directories.
Step 5 – Now that you have the directory set up, where you store your batch files, here is how you can create one. Leave the command prompt window open and click on the Windows Start button again. This time enter Notepad and click on the search result to open the Notepad program. Notepad is a simple text editor but ideal for creating batch scripts with.
Step 6 – To create your first batch file, enter the following into Notepad:
echo Hello World!
By default, a batch file displays all the commands that it runs through, line by line. What the @echo off command does is turn that feature off for the whole script; with the ‘@’ (at) sign to apply that command to itself.
Step 7 – When saving anything in Notepad the default extension is .txt, to denote a text file. However, you want the extension to be .bat. Click on File > Save As and navigate to the newly created Batch Files directory in Documents. Click the drop-down menu Save as Type, and select All Files from the menu. In File Name, call the file Test.bat.
Step 8 – Back at the command prompt window, enter: dir/w again to list the newly created Test.bat file. By the way, the /w part of dir/w means the files are listed across the screen as opposed to straight down. Enter dir if you want (although you need more files to view) but it’s considered easier to read with the /w flag.
Step 9 – To execute the batch file you’ve just created, simply enter its name, Test, in the command prompt window. You don’t need to add the .bat part, as Windows recognises it as an executable file, and the only one with that particular name in the current directory. Press return and see how you’re greeted with Hello World! in the command prompt.
Step 10 – The echo command displays whatever is after it to the screen. Right-click the Test.bat file from Windows Explorer and select Edit to add more echo commands if you like. Try this:
echo Hello World!
echo This is my first batch file
echo With a blank line between!
Remember to save each new change to the batch file.