How to Create a Batch File?

Affiliate disclosure: In full transparency – some of the links on our website are affiliate links, if you use them to make a purchase we will earn a commission at no additional cost for you (none whatsoever!).

If you need a helping hand getting your desired thing done in your Computer, Batch files come handy. You can use them to do daily tasks in a snap, and this will save your time too.

Anyone can operate a Batch file, and there is no hard part in executing it. Some people never had any interest in this, causing they don’t know about it.

What is a batch file: a computer file containing a list of instructions to be carried out in turn.

Since automation programs like AutoHotKey exist, many people have never written or taken the time to understand bat files, and many don’t even know what they do.

In this post, I’m going to show you How to create a batch file and guide through the basic of what is needed to write a batch file. After reading this post, you’ll get an idea on how I’ll also help you with few resources for learning how to make a batch (.bat) file in case you’d like to go further.

How to Create a Batch File

How-to-create-a-batch-file

Ok, let’s assume that you are having a problem with your network,  one way to find out the problem is to type ipconfig and pinging Google to see if there is such a problem causing the network problem.

Obviously, this can be a bit annoying when you have to do this more often. So what we can do to automize the task? We can make a batch file and stick it to USB drive so whenever you encounter in network issue. Plug the USB drive and run the batch file, as simple as that

Creating A BAT File

Creating a bat file in super duper easy, follow these steps.

Right-click on your desktop and click on NEW – Text document. Double click on it, it will be completely blank. Next, go to File menu and choose “SAVE AS”, a window box will prompt out. Type a filename and at the end add “.bat.” My file name was TestBat.Bat for example.

Also, make sure windows do not save this file as a text file. You need to set the ‘save as types’ dropdown to ‘ALL files’, then windows will take the .bat name as an extension.

how-to-make-a-batch-file

Do Some Coding

If you want to create your own bat files, you need to learn how to run a command in Command Prompt program in windows. Because you can speed up the process if you know that, both languages are same. A batch file tells command prompt to do things, and this saves human time and effort.

There are many commands, but these are the basic commands that matter most. Let’s learn about them first.

Title: Name of the Bat file. Yes, you can name it anything you like.

ECHO – In bat files, this is the “print” statement for BAT files. Whatever you give following the ECHO word, it will appear in the command prompt as well.

ECHO OFF – Hide the command while the program is running.

PAUSE – This yields the “press any key to proceed… ” message that you’ve seen very commonly. It’s useful because it delays the BAT document execution until the client instructs it to go once more. If you don’t place this in your system, everything will speed by and end before you can see it. Individuals regularly place this in BAT documents to allow the client to survey the material on the screen before proceeding.

CLS – Clears the DOS window (supportive if things get excessively messed!).

IPCONFIG – Network information.

PING – Ping a IP address to confirm whether you are connected to the internet or not.

Do Some Quick Logic

Before you start, We have to arrange our project out. Any great software engineer will consider the general structure of their system before they dash into things – it keeps them from committing rationale errors that are difficult to retreat from.

IN this post there are two examples/ projects. In 1st project, we need to check the PC’s system network/web settings with an “ipconfig/all” command, and afterward audit that data by giving the client time to peruse everything.

The second example is a script that will remove specific words or characters from multiple files at once. You need to save the second batch file in the same directory where you want to delete that particular word.

Write Your BAT File

Okey, Now Right click on your desired Bat file and choose “EDIT” to open it in Notepad. (You need to click right and select Edit to open the bat file in notepad.)

Code Example 1

———–Start Code———–

ECHO OFF
ECHO As a network admin, I’m getting tired of having to type these commands in! Hopefully, this saves me some time in the long run.
IPCONFIG /ALL
PAUSE
PING www.google.com
ECHO All done pinging Google.
PAUSE
———–End Code———–

Code Example 2.

This is another example of a script that removes specific words from mp3 files at once.

Assume you a mp3 folder in this format: Song1 SiteName.mp3 and so on. You can remove the SiteName from all of the files in just one click. Here is how it goes.

–Code Start–

SETLOCAL enabledelayedexpansion
SET “word= [SiteName]”
IF “%word%”==”” GOTO :EOF
FOR /f “delims=” %%a IN (‘dir /a-d /b “*%word%*.mp3″‘) DO (
SET “fname=%%~na”
SET “fname=!fname:%word%=!”
IF NOT “!fname!”==”” REN “%%~a” “!fname!%%~xa”
)
pause

–Code End–

NOTE: Replace SiteName with anything you want to remove from multiple files at once.

Test Or Run the BAT File

Whatever you have in a bat file, you coded, or you copy-pasted it from somewhere else. Save the file and make a backup of this file. Open it by double clicking it, and The output will be similar as below image.

how-to-create-a-bat-file

Here it goes example 2nd test.

how-to-write-a-batch-file

Bang! That’s how to create a batch file, and you did it successfully.

You can learn more about batch files, here are the commands that you can run in command prompt. Using them, you can write your own batch file. Here are other examples you can learn online. Any question related to the batch file, scroll and comment it right now. (PS: Don’t ask command related question here.)

In my case I made the script that removes certain words from files at once, It actually help me categorize files and keep my computer easy to find something. I keep this batch file on my USB drive, and whenever I see lots of files contains certain words (like a website name) I plug the USB drive and runs the bat file.It does do the work in a snap.

Have you written anything cool with BAT files before? Try a new one.

devendra

Leave a Comment