Table of Contents
Make your own batch file and see the magic.
Batch file is a term that Windows users often hear. Basically, it is a way of doing things without actually doing them. What?
Confused! Wait don’t take me wrong, let me explain what a batch file is, how it works and what can you do with a batch file.
So, what exactly is a batch file?
As a Windows user you must be aware of the Command prompt or CMD. Using it you can give various commands and make things simpler. But most Windows users aren’t comfortable using it.
Hence we have a batch file with .bat,. btm, .cmd file extensions with CMD commands.
In simpler words batch file is an intermediary between you and the command prompt. When a batch file is executed the commands written in it are executed in a serial fashion. If batch files aren’t used, we will have to manually enter each command line by line.
Now, usage of batch file.
Why do we use Batch File?
The answer is simple: a batch file saves the most precious thing i.e. time, otherwise, we would have to type the same commands every now and then. For example, using a batch file you can schedule Windows shut down.
If a batch file is created with commands to perform a shutdown operation, what you’ll have to do is simply double click on it and run it. This will shut down Windows at the specified time.
This is the tip of the iceberg, there’s more batch files can do.
Batch files are useful for program developers also, as they help automate a process.
For example while installing a program if command prompt on your machine needs to be used, they will include a batch file in the setup files. If they would have not done this, you’ll have to run commands manually and that won’t make you happy.
A batch file helps enable loops (for), conditional statements (if), control statements (goto), etc. To run a batch file you simply need to type its name in command prompt or can run one batch file from another by using CALL command.
Furthermore, batch files have different modes on the basis of which the program works.
What are batch file modes?
Batch file mode reads text file that contains one or more directives. A batch file where selection is required to execute is called interactive mode. For example, when you a user needs to give an input to proceed further like Yes or No.
Apart from this, there is another batch mode where bat files run without asking for human interaction.
So, this is a small piece of information about batch files modes and how they are different. Now, let’s move ahead and learn how to create batch files.
But before that you need to learn a few things to create batch files.
Basic batch file commands that will help you create batch files?
@echo – command will allow you to see the output of the strings being passed as arguments. This command is used in batch file and shell scripts. Using this parameter you can see your code working. An off to this parameter will close the script as it is finished.
Title – Gives title to your batch script in CMD window.
cls – clears Command Prompt.
pause – is used to give a break. The phrase “Press any key to continue…” denotes a pause.
rem – short for remark it is used to enter a remark into the code.
Start “” [website] – will open website of your choice in the default web browser.
Ipconfig: classic command to get network information like MAC address, IP address etc.
:: – used to add a comment in the batch file. Any text written with a comment is ignored by the Command Prompt.
%%a: Each file in the folder.
(“.\”) Root folder. When using the command prompt. Paste the .bat file into the directory and see the magic.
EXIT – To come out of the Command Prompt.
So, these are internal commands in addition to this batch script also supports external commands.
Now, let’s learn how to create a batch file in windows 10.
Before that let’s create a RAW bat file.
1. Open Notepad or WordPad in your Windows 10.
2. Add commands, starting with @echo[off], followed by echo <this is the best file> pause.
3. Save the file with .bat file extension, for example you first.bat.
4. Now double click the batch file to run it.
5. To edit the bat file right click > Edit.
Raw bat file will look like:
And the output will be:
Steps to create a BAT file on Windows 10
Here, we are going to discuss steps to create a BAT file. The steps will work on all version of Windows be it Windows 10, Windows 8.1, 8, Windows 7 or older.
1. Open a new Notepad file. Alternatively you can use Notepad++ or WordPad.
2. Now, type the following commands:
title My first test file
Title tag provides title for the batch script in Command Prompt window.
echo First test file executed.
echo I don’t like typing commands again and again.
Save the text file with the extension .bat instead of .txt. For example test.bat.
Note: If you are unable to change the file extension turn off Hide File Extensions feature.
Now, double click the batch file to run it. This will open CMD window where you can see the output.
Now, try creating batch file with echo on and see what it does. Every time you’ll run this file the same text will be displayed.
Want to open your favorite sites at a go using bat files?
Yes, so here we go.
start “” https://tweaklibrary.com/
start “” http://blogs.systweak.com/
start “” https://www.systweak.com/
start “” https://www.tweaking.in/
Once you have entered your favorite site Save the file. File > Save As > give name to the file > remember to save file with .bat extension.
Now double click on the BAT file, this will immediately open the sites mentioned in BAT file.
So, here we go using Batch files you can even organize your files.
How Batch file helps declutter Download Folder?
You download a number of files daily and this messes your Download folder, right? Nothing to worry about using a batch file you can arrange all the files by file type. Use the following batch file and see the magic.
Note: You’ll need to place this batch file into the disorganized folder.
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\”
This script will help organize your Download folder or whichever folder you want to manage and have placed the .bat file in.
This was just a taste of what batch scripts can do. If you need to something over and over again without typing in much the batch files are your way to go. These files will not only make things simpler for you but will also make you flaunt your skills in front of your family and friends. Furthermore, to editing a batch file is simple. To modify or to make changes in it right click the file > Edit. This will open the file in the editor make changes and save it.
Hope you have now understood what a batch is file, how to create a batch file and the most common command you can use in a batch file.
If you have something to say let us know in the comments.