Creating a batch file to automate repetitive tasks is easy, once you know how.

Batch files are the computer handyman's way of getting things washed. They can automate everyday tasks, shorten the required fourth dimension to practice something, and interpret a circuitous process into something anyone could operate.

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

How to Create a Batch File in Windows

Before going into the details, here is a quick summary:

  1. Open a text file, such every bit a Notepad or WordPad document.
  2. Add your commands, starting with @echo [off], followed past, each in a new line, title [title of your batch script], echo [starting time line], and pause.
  3. Relieve your file with the file extension BAT, for example, exam.bat.
  4. To run your batch file, double-click the BAT file y'all just created.
  5. To edit your batch file, correct-click the BAT file and select Edit.

Your raw file will look something like this:

A simple batch file with the most basic elements.

And hither'due south the respective control window for the example above:

Test BAT CMD Prompt

If this was likewise quick or if you want to learn more nigh commands and how to use them, read on!

Pace i: Create a BAT File

Let's say that y'all oftentimes have network issues; yous constantly find yourself on the command prompt, typing in ipconfig, and pinging Google to troubleshoot network issues. After a while, you lot realize that it would be a bit more than efficient if y'all just wrote a simple BAT file, stuck it on your USB stick, and used it on the machines yous troubleshoot.

Create a New Text Certificate

A batch file simplifies repeatable estimator tasks using the Windows command prompt. Below is an case of a batch file responsible for displaying some text in your command prompt. Create a new BAT file by right-clicking an empty infinite inside a directory and selecting New, so Text Certificate.

Open New Text File in Windows

Add together Code

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

          @echo off
championship This is your outset batch script!
echo Welcome to batch scripting!
pause

Salve As BAT File

The in a higher place script echoes back the text "Welcome to batch scripting!" Save your file by heading to File > Save As, and then proper name your file what you'd similar. End your file name with the added BAT extension, for example welcome.bat, and click OK. This will finalize the batch process. Now, double-click on your newly created batch file to activate information technology.

Test BAT CMD Prompt

Don't assume that's all batch scripting tin can do. Batch scripts parameters are tweaked versions of command prompt codes, so you are merely express to what your command prompt can do. For those unfamiliar with the program, the control prompt is capable of quite a lot.

Stride ii: Acquire the Basics of Batch Scripting

Batch files apply the same language every bit the command prompt. All y'all're doing is telling the command prompt what y'all want to input using a file, rather than typing information technology out in the command prompt. This saves you time and attempt. It as well allows you lot to put in some logic, like elementary loops, conditional statements, etc. that procedural programming is capable of conceptually.

@repeat: This parameter volition let you lot to view your working script in the command prompt. This parameter is useful for viewing your working lawmaking. If whatever bug ascend from the batch file, you volition be able to view the bug associated with your script using the echo office. Adding a following off to this parameter will allow you to speedily shut your script after it has finished.

  • title: Providing much of the same part as a <title> tag in HTML, this volition provide a title for your batch script in your Command Prompt window.
  • cls: Clears your control prompt, best used when extraneous lawmaking can make what y'all'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 directly the prompt to a particular directory before irresolute a files name, deleting a file, and and then on. With batch files, yous only need to paste your BAT file into the directory of your choosing.
  • break: Allows a break in the logical concatenation of your BAT file. This allows for users to read over command lines earlier proceeding with the lawmaking. The phrase "Printing whatsoever primal to continue..." will denote a pause.
  • beginning "" [website]: Will head to a website of your choice using your default web browser.
  • ipconfig: This is a archetype command prompt parameter that releases information concerning network information. This data includes MAC addresses, IP addresses, and sub-net masks.
  • ping: Pings an IP accost, sending data packets through server routes to guess their location and latency (response time).

The library for batch variables is huge, to say the least. Luckily in that location is a Wikibook entry that holds the all-encompassing library of batch script parameters and variables at your disposal.

Step three: Write and Run Your BAT File

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

News Script

Let'southward create an immediately useful batch script. What if you wanted to open all your favorite news websites the moment you lot wake upwards? 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 procedure: outset, 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 case will provide the main American news media outlets available online.

          @echo off
cd "" http://www.cnn.com
kickoff "" http://www.abc.com
start "" http://www.msnbc.com
commencement "" http://www.bbc.com
first "" http://www.huffingtonpost.com
outset "" http://world wide web.aljazeera.com
start "" https://news.google.com/

The higher up script stacks ane start "" parameter on top of the other to open multiple tabs. You can replace the links provided with ones of your choosing. Later on you've entered the script, caput to File, then Save As. In the Save Every bit window, save your file with the BAT extension and modify the Salvage as blazon parameter to All Files (*.*).

Saving a Text File as a BAT File

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

File Organizer

Accept you been downloading multiple files a solar day, only to have hundreds of files bottleneck up your Download folder? Create a batch file with the post-obit script, which orders your files by file blazon. 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 cheque if extension binder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
motion "%%a" "%%~dpa%%~xa\"
))

Here is an case of my desktop before, a loose assortment of files types.

Messy Windows Desktop

Here are those same files subsequently.

Organized Windows Desktop

It'south that simple. This batch script volition also work with any blazon of file, whether it's a document, video, or audio file. Fifty-fifty if your PC does not support the file format, the script will create a folder with the advisable label for you lot. If y'all already accept a JPG or PNG folder in your directory, the script volition only move your file types to their appropriate location.

Plan Launcher

If you find yourself opening the same set of apps over and over again, y'all can now create a custom launcher batch file that opens them all with a single click. All y'all demand to notice out is the Windows file location. Let's say you need to do some work, and you want to open Excel, the Reckoner, and Spotify. Hither'due south the code for that:

          @repeat off
cd "C:\Program Files\Microsoft Officeoot\Office16\"
start EXCEL.EXE

You could even have your batch file open specific documents or websites, along with a set of apps. The trick is to mix-and-match all the different elements a batch file can do for you. Eventually, you'll exist incorporating IF statements into your batch scripts.

Automate the Simple Stuff With Batch Scripts

This is simply a taste of what batch scripts have to offer. If yous need something simple done over and over, whether information technology be ordering files, opening multiple web pages, renaming files en masse, or creating copies of important documents, you can make tedious tasks simple with batch scripts.

How to Repair a Corrupted Windows 10 Installation

Read Adjacent

Nearly The Author