How-To Write Batch Files

How-To write Batch Files in Windows

Windows uses a Graphical User Interface (GUI) to make the process of using the computer much easier. However, in the old days of Windows, the whole interaction between a user and the computer was done using written text commands. While a console based approach may be faster in many ways and is still preferred by many professionals for development purposes, it is also extremely hard to master and comes with a very steep learning curve.

Today, most basic tasks in Windows can be performed through a graphical interface using a mouse. But one big downside of this method is that it is not possible to automate tasks this way, unless Windows gives you a built-in process or tool to allow you to achieve this. Fortunately, there is still a way for you to write commands and have them executed by Windows in the form of batch files.

What is a Batch File?

As defined by Wikipedia, a Batch file is a form of script file written in Microsoft’s DOS, OS/2 and Windows line of operating systems. It stores a series of commands in an executable form, and can be executed by the command-line interpreter. One key characteristic of batch files is that they are stored in plain text. The term batch comes from batch processing, which means ‘non-interactive execution’.

Windows provides the functionality to run batch files in order to make it easier to run mundane tasks by allowing the user to write a script to automate them. Most batch files can be identified by the .bat extension (used in DOS and Windows). However, Windows NT and OS/2 also have support for the .cmd file extension. There is also a third file extension, .btm, which is used by 4DOS, 4OS2, 4NT and Take Command and are somewhat faster as the whole script is loaded entirely ready for execution instead of line-by-line execution.

How-To Write Batch Files

A batch file is written in plain text. If you have any experience with a programming language, the process will seem pretty simple. You can easily write a batch file with any text editor, even the default notepad program in Windows. Some basic commands include,

  • GOTO – Jump to a label or subroutine in your batch file
  • ECHO – display text on the screen
  • GETMAC – Display MAC address for local or remote network adapters (XP and later)
  • START – Run a command in a separate process, or run a file with its default associated application (can be used to start an application like chrome)

There are hundreds of commands that you can use to write a batch file, most of which can be found over here. Here, we will make use of some basic commands to give a few examples of different kinds of tasks you can perform using a batch file script,

  • ‘Hello World’ Batch File

The most common form of a simple program in any programming language is a ‘Hello World’ program. Aptly named, it simply shows of how to print a single statement in any scripting/programming language by printing ‘Hello World’ on the screen. Here’s how you can do it with a batch file,

Open a new notepad file and paste the following text,

@echo off

echo ‘Hello World’

pause

exit

Save the notepad file as ‘HelloWorld.bat’. You can name it anything you like as long as you remember to keep the extension as ‘.bat‘. Once you save it, it should show the following icon.

Just double-click on the icon to run the executable file, and it should show the following output screen.

In this program, we made use of only three commands. Here’s a breakdown of what each one does.

  1. @echo off – By default, the Command Prompt is set to display all the commands that are currently being executed. It can be turned off by using this command.
  2. echo ‘Hello World’ – You can use echo followed by any statement in inverted commas to print it on the console.
  3. pause – This tells the console to wait indefinitely until the user gives any kind of input signal for the program to progress further. If you don’t insert the pause command, it will execute the complete code but it will do it in the blink of an eye and close the output command shell and you won’t be able to see the output displayed on the screen.
  4. exit – gracefully terminates a batch file
  • Starting multiple Programs

You can easily use a batch file to start a program or even open a particular folder. The main advantage of using a batch file to do this is that it allows you to launch multiple programs instantly. Here’s how you can do it,

Open a new notepad file and paste the following text,

@echo off

start chrome

start notepad

start downloads

exit

This program will open Google Chrome (if installed on your PC), Notepad, and open the Downloads folder. If the above code doesn’t work smoothly for you for launching other programs, you can also try changing the directory and manually locating the executable file for that program by using the following approach (shown for Google Chrome),

@echo off

cd “C:\Program Files\Google\Chrome\Application\”

start chrome.exe

exit

  • Deleting Files Older than X Days

You can also use a batch file to delete all the files in a given folder that are older than a variable number of days, where you can set X depending on your requirements. You can use the following line of code to achieve this,

forfiles /p “C:\some\file\name\here” /s /m * /d -5 /c “cmd /c del @path”

The above line of code will delete all the files that are older than 5 days. You can change the 5 in the line to change the number of days.

Note: You can also paste the line in your Command Prompt to execute it. But saving the command in the form of a batch file allows you to quickly execute it by just double-clicking it.

  • Changing your IP address Scheme

A batch file can be used to quickly set up a static IP addressing scheme (normally, the IP address is dynamically assigned). You can use the following line of code to achieve this,

netsh interface ip set address “LAN” static “xxx.xxx.xxx.xxx” “xxx.xxx.xxx.x” “xxx.xxx.xxx.x”

Note: Replace the x’s with the required static IP.

To change the settings back to dynamic addressing, execute the following line of code,

netsh int ip set address name = “LAN” source = dhcp