btach-file-windows

You may have heard Windows users often throwing the term “batch file” when discussing files. It’s a way of doing things without actually doing them, and no, we’re not toying with you. In this article, let’s look at what is a batch file and a basic idea of batch file programming.

Windows has a lot of file types, and it gets confusing too soon. There are exe’s, txt’s, appx’s, and whatnot. However, they’re not very hard to understand and get a gist of.

So, what is a batch file exactly?

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 Windows users, most of us aren’t much comfortable with anything that looks a bit suspicious (Thanks for the good ‘ol Viruses times, Microsoft), 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, and .btm file extensions containing a set of Windows commands. When you run a batch file, the commands written in them are executed in the Command Prompt in a serial fashion, similar to .SH on Linux. The set of commands is also known as a batch script, which solves the need to enter each command line by line.

What’s the use of a batch file?

It’s only natural to ask, “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 usually 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 we’d 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.

What are batch file modes?

You might have done it many times, but there are batch files in which you need to make a selection to continue execution.  For example, it may ask you Yes or No in order to proceed further.

This is called interactive mode, where input from the user is required. The other mode is called batch mode, where a bat file keeps doing its work without disturbing the user.

Batch file commands: Batch 101

Before you learn how to make a batch file in Windows, you should be aware of a few things. Creating a batch file is all about commands and crafting them appropriately for best use. You need to know some batch file commands which will help you create basic batch files.

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

echo – Displays the input string as the output. Use the 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 a Windows batch file.

EXIT – To exit the Command Prompt.

cls – Used to clear the command prompt screen.

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

These are what we call internal commands, which are shipped in Windows. Your batch script also supports external commands. These are the ones that are added when new software is installed on your system. For example, if you have Google Chrome installed on your machine, you can use the command ‘chrome’ in the CMD window.

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

How to create a batch file in Windows 10?

Here are the steps to create a small batch (BAT) file. The steps are almost the same, no matter which Windows version you’re running.

  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:
    batch-file-txt2
    echo off

    title My Test 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.

How to run a batch file in Windows 10?

In addition to doubling-clicking, you can execute a bat file from within the CMD window. To open a file using cmd, you need to navigate the folder/directory where the file is located. Then type the name of that file along with its file extension. For instance, you need to run a bat file named HelloWorld. Type HelloWorld.bat and press Enter.

How to edit or modify a batch file?

If you want to experiment with a BAT file you created in the past, editing a bat file is a simple process. Just right-click the file and click on rename. It’ll open in the text editor.

After you’re done, save the changes, and you’re good to go. The next time you run the bat file in the command line, it will execute the changes you have made.

Do something more – BAT file examples

Some time ago, I told you about checking the battery health on your Windows machine by using the POWERCFG utility. Earlier, you had to do a lot of work. In order to check the battery’s 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 automating the task.

To generate a Energy Report:

In a new text file, type the following command:

powercfg/energy
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.

To generate a 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 the version of the report which is already stored at that location. Using batch files with admin rights will display the latest data.

So, this was the brief walkthrough of creating a batch file in Microsoft Windows. You can use the A to Z Windows CMD Commands List to make your Windows batch files as per requirement. If you have something to add, tell us in the comments below.

Similar Posts