0% found this document useful (0 votes)
268 views

Creating Batch Files

The document provides instructions on how to create batch files in Windows. It explains that batch files allow storing commands that can then be executed sequentially by the command line interpreter. The summary then provides step-by-step instructions on how to create a basic batch file to automate simple tasks like changing the command prompt title or generating a system report. Examples of more advanced batch files that can organize files or open multiple websites are also given.

Uploaded by

sean12266727
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views

Creating Batch Files

The document provides instructions on how to create batch files in Windows. It explains that batch files allow storing commands that can then be executed sequentially by the command line interpreter. The summary then provides step-by-step instructions on how to create a basic batch file to automate simple tasks like changing the command prompt title or generating a system report. Examples of more advanced batch files that can organize files or open multiple websites are also given.

Uploaded by

sean12266727
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

What Is A Batch File In Windows?

How To
Create A Batch File?

Short Bytes: In Windows, the batch file stores commands in a serial order to be used by the
command line interpreter CMD as an input. In this article, I have mentioned the basic steps
by which you can create a batch file of your own in order to automate tasks on your
Windows machine.

What exactly is a batch file?

The batch file is a term, which often falls on the ears of Windows users. It is a way of doing
things without actually doing them. Wait, before you ask me whether I am high, let me
elaborate the batch file and its working.

You might be aware of the Windows command line interpreter known as CMD or Command
Prompt. It takes various commands as input through the keyboard and processes them.
Being a Windows user, most of us are not much comfortable with anything that doesn’t look
good, and CMD is one of them.

A batch file does the work of a mediator between you and the command prompt. It is a file
– with .bat, .cmd, .btm file extensions – containing the CMD commands. When you run a
batch file, the commands written in it are executed in the Command Prompt following a
serial fashion. Otherwise, these would have to be entered manually line by line.
What Is A Batch File In Windows? How To
Create A Batch File?

What’s the use of a batch file?

Now, why should you keep the commands in a batch file? A batch file saves your time,
otherwise, would be invested in typing the same commands again and again. For instance,
you can schedule your Windows OS to shut down after a specific amount of time using the
command prompt. If you have created a batch file for the shutdown operation, you’ll only
have to double-click to run it, like you normally open applications, and your Windows will
shut down after the time you have already set.

If a developer wants to use the command prompt on your machine while installing software,
he can do so by including a batch file in the setup files. Otherwise, you would have to run
the commands which I guess won’t make you happy. In a nutshell, it is a script file used to
automate tasks in DOS, Windows, and OS/2 operating systems.

While creating a batch file, you can also enable loops (for), conditional statements (if),
control statements (goto), etc. You can run a batch file directly from the command prompt
by typing its name. Also, you can run one batch file from another batch file using the CALL
command.

Things to know before creating a batch file

Creating a batch file is all about commands and crafting them appropriately for best use.
You need to be aware of some basic Windows CMD Commands which will help you create
basic batch files.

title: It used to change the title text displayed on top to CMD window.

echo – Displays the input string as the output. Use ON or OFF option for ECHO to turn the
echoing feature on or off. If you turn on the ECHO, the CMD will display the command it is
executing.

pause – Used to stop the execution of Windows batch file.

EXIT – To exit the Command Prompt.

cls – Used to clear the command prompt screen.


What Is A Batch File In Windows? How To
Create A Batch File?

:: – Add a comment in the batch file. The Command Prompt ignores any text written as a
comment.

To open a file using cmd, you need to navigate to that folder/directory using the command
line. Then type the name of that file along with its file extension. For instance, you need to
run a text file named HelloWorld. Type HelloWorld.txt and press Enter.

These commands can help you to create a basic batch file. You can improve your batch file
by learning more commands from the Windows CMD Commands list.

How to create a batch file in Windows?

Here, I am going to tell you steps to create a small batch file.

1. Open a new notepad file. You can also use any similar text file editor, like
Notepad++.
2. Type the following commands in the text file:

echo off

title My Test Batch File


What Is A Batch File In Windows? How To
Create A Batch File?

:: See the title at the top. And this comment will not appear in the command prompt.

echo Test file executed.

echo I am too lazy to write commands again and again.

pause

3. Save the text file with the extension .bat instead of .txt. For instance, testbatch.bat
in my case.
Note: Make sure the Hide File Extensions feature is turned off in Windows.
Otherwise, you will not be able to change the file extension. To check, go to Control
Panel > File Explorer Options > View tab > Uncheck Hide extensions for known file
types.
4. To Run the batch file, simply double-click it. The CMD Window will open
automatically with the desired output.

You can also try echo on in the command mentioned in Step 2 to see what it does. Now,
every time you’ll run this file the same text will be displayed. You can also use the
.cmd extension in place of .bat extension.

Do something more

You can check the battery health on your Windows machine by using the POWERCFG utility.
Previously, you had to do a lot of work. In order to check the battery health, you had to
open CMD, type the command, and then go to the location where the output file gets
stored.

Let’s give some rest to your hands by creating a batch file and automate the task.

Create a batch to generate Energy Report:

In a new text file, type the following command:

powercfg/energy
What Is A Batch File In Windows? How To
Create A Batch File?

C:\WINDOWS\system32\energy-report.html
Save the file as energyreport.bat or any name you like. Keep in mind you use the correct file
extension.

Create batch file to generate Battery Report:

In a new text file, type the following commands:

powercfg /batteryreport
C:\Windows\System32\battery-report.html
Save the file as batteryreport.bat or any name you like.

Important: The POWERCFG utility only works with administrator privileges. So, you will have
to run these batch files in “Run As Administrator” mode. You do so by Right Clicking the
batch file > Click Run As Administrator.

If you run these battery checking batch files without administrator privileges, it will show
you version of the report, which is already stored at that location. Using batch files with
admin rights will display the latest data.
What Is A Batch File In Windows? How To
Create A Batch File?

News Script
Let us create an immediately useful batch script. What if you wanted to open all your
favorite news websites 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 (*.*).
What Is A Batch File In Windows? How To
Create A Batch File?

Once you’ve 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" (
What Is A Batch File In Windows? How To
Create A Batch File?
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 my desktop before, a loose assortment of image files.

Here are those same files afterward.

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.

You might also like