Pinterest Stumbleupon Whatsapp
Advertisement

Batch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate.

In this article, I’ll show you how to write a simple batch file. You’ll learn the basics of what batch files can do and how to write them yourself. I’ll also provide you with further resources for learning to write batch (BAT) files.

Step 1: Create a BAT File

Let’s say that you frequently have network issues; you constantly find yourself on the command prompt A Beginners Guide To The Windows Command Line A Beginners Guide To The Windows Command Line Read More , typing in ipconfig and pinging Google to troubleshoot network problems. After a while you realize that it would be a bit more efficient if you just wrote a simple BAT file, stuck it on your USB stick, and used it on the machines you troubleshoot.

Create a New Text Document

A batch file simplifies repeatable computer tasks using the Windows command prompt. Below is an example of a batch file responsible for displaying some text in your command prompt. Create a new BAT file by right-clicking an empty space within a directory and selecting New, then Text Document.

new_text_file

Add Code

Double-click this New Text Document to open your default text editor. Copy and paste the following code into your text entry.

Advertisement

@echo off
title This is your first batch script!
echo Welcome to batch scripting!
pause

Save As BAT File

The above script echoes back the text “Welcome to batch scripting!”. Save your file by heading to File, Save As, and then name your file what you’d like. End your file name with the added .bat extension — welcome.bat for example — and click OK. This will finalize the batch process. Now,  double-click on your newly created batch file to activate it.

welcome_to_batch

Don’t assume that’s all batch scripting can do. Batch scripts parameters are tweaked versions of command prompt codes, so you are only limited to what your command prompt can do. For those unfamiliar the program, command prompt is capable of quite a lot 15 CMD Commands Every Windows User Should Know 15 CMD Commands Every Windows User Should Know The command prompt is an antiquated, but powerful Windows tool. We'll show you the most useful commands every Windows user needs to know. Read More .

Step 2: Learn Some Quick Code

If you know how to run commands in the command prompt, you’ll be a wiz at creating BAT files because it’s the same language. All you’re doing is telling the command prompt what you want to input through a file, rather than typing it out in the command prompt. This saves you time and effort; but it also allows you to put in some logic (like simple loops, conditional statements, etc. that procedural programming is capable of conceptually).

@echo — This parameter will allow you to view your working script in the command prompt. This parameter is useful for viewing your working code. If any issues arise from the batch file, you will be able to view the issues associated with your script using the echo function. Adding a following off to this parameter will allow you to quickly close your script after it has finished.

title — Providing much of the same function as a <title> tag in HTML, this will provide a title for your batch script in your Command Prompt window.

cls — Clears your command prompt, best used when extraneous code can make what you’re accessing had to find.

rem — Shorthand for remark, provides the same functionality as <!– tag in HTML. Rem statements are not entered into your code. Instead, they are used to explain and give information regarding the code.

%%a — Each file in the folder.

(“.\”) — The root folder. When using the command prompt, one must direct the prompt to a particular directory before changing a files name, deleting a file, and so on. With batch files, you only need to paste your .bat file into the directory of your choosing.

pause — Allows a break in the logical chain of your .bat file. This allows for users to read over command lines before proceeding with the code. The phrase “Press any key to continue…” will denote a pause.

start “” [website] — Will head to a website of your choice using your default web browser.

ipconfig — This is a classic command prompt parameter that releases information concerning network information. This information includes MAC addresses, IP addresses, and sub-net masks.

ping — Pings an IP address, sending data packets through server routes to gauge their location and latency (response time).

The library for batch variables is huge, to say the least. Luckily there is a Wikibook entry which holds the extensive library of batch script parameters and variables at your disposal.

Step 3: Write & Run Your BAT File

We’ll create two examples of batch scripts which can simplify your daily online and offline activities.

News Script

Let’s create an immediately useful batch script. What if you wanted to open all your favorite news websites Read More Intelligent Content in 2016 with These 35 Sites Read More Intelligent Content in 2016 with These 35 Sites We should all read these 35 sites more often. If you are tiring of dumbed-down content make things somewhat more thoughtful this coming year with this super list. Read More the moment you wake up? Since batch scripts use command prompt parameters, we can create a script that opens every news media outlet in a single browser window.

To re-iterate the batch-making process: first, create an empty text file. Right-click an empty space in a folder of your choosing, and select New, then Text Document. With the text file open, enter the following script. Our example will provide the main American news media outlets available online.

@echo off
start "" http://www.cnn.com
start "" http://www.abc.com
start "" http://www.msnbc.com
start "" http://www.bbc.com
start "" http://www.huffingtonpost.com
start "" http://www.aljazeera.com
start "" https://news.google.com/

The above script stacks one start “” parameter on top of the other to open multiple tabs. You can replace the links provided with ones of your choosing. After you’ve entered the script, head to File, then Save As. In the Save As window, save your file with the .bat extension and change the Save as type parameter to All Files (*.*).

save_as_bat

Once you’d saved your file, all you need to do is double-click your BAT file. Instantly, your web pages will open. If you’d like, you can place this file on your desktop. This will allow you to access all of your favorite websites at once.

File Organizer

Have you been downloading multiple files a day, only to have hundreds of files clogging up your Download folder? Create a batch file with the following script, which orders your files by file type.  Place the .bat file into your disorganized folder, and double-click to run.

@echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ ""  if "%%~dpxa" NEQ "%~dpx0" (
rem check if extension folder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
move "%%a" "%%~dpa%%~xa\"
))

Here is an example of the my desktop before, a loose assortment of image files.

assorted_files

Here are those same files afterward.

ordered_files

It’s that simple. This batch script will also work with any type of file, whether it’s a document, video, or audio file. Even if your PC does not support the file format, the script will create a folder with the appropriate label for you. If you already have a JPG or PNG folder in your directory, the script will simply move your file types to their appropriate location.

Automate the Simple Stuff

This is just a taste of what batch scripts have to offer. If you need something simple done over and over — whether it be ordering files, opening multiple web pages, renaming files en masse How to Batch Rename & Mass Delete Files in Windows How to Batch Rename & Mass Delete Files in Windows Are you pulling your hair out over how to batch rename or delete hundreds or thousands of files? Chances are, someone else is already bald and figured it out. We show you all the tricks. Read More , or creating copies of important documents — you can make tedious tasks simple with batch scripts.

Do you use batch scripts? If so, what task do you automate? Let us know in the comments below!

Originally written by Paul Bozzay on March 24, 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *