A batch file in Windows is a way to automate Windows tasks. Batch files contain the extension .bat. To create a batch file, create a new file in a text editor such as Notepad, enter your commands, and save the file with a .bat extension, e.g. tasks.bat.
Batch files are useful in that you don’t have to repeat typing a series of Windows commands such as running executables – they are done automatically through the file.
Let’s go through an example of using a bat file.
First, create the file in Windows:
Edit the file in Notepad:
Now, let’s add some commands. Let’s add a command to display Hello World:
echo Hello World
You will see the output:
Now let’s write 2 lines:
echo Hello World echo Hello World Again!
This will output:
Note as we have 2 lines with echo, this runs on 2 prompts.
Now we will add echo off. This will prevent the word “echo” from being displayed:
echo off echo Hello World echo Hello World Again!
Here, since we have echo off, the command “echo Hello World” does not display the word echo, and only displays the words “Hello World” and Hello World Again. Also this is all under the same prompt:
However, this still includs the words “echo off”. Let’s say we want to supress this. We can do this by using the @ symbol. So the command:
@echo off echo Hello World echo Hello World Again!
Produces:
Note you can also use the command @echo on.
Let’s say we want to run an application. We can do that using the start command. For example, if we want to start notepad, we can do it like below:
@echo off echo Hello World start notepad echo Hello World Again!
To open an application with parameters, enter:
start notepad C:\test.txt
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL