You might have heard about the purpose of a batch file but have no clue what it means and how to create one.
If that’s true, this piece will address this issue and intimate you with what is meant by a batch file and how can you get the same in Windows 10.
Let’s start with the basic details!
What Is A Batch File?
Are you aware of Windows command line interpreter that is commonly known as Command Prompt or CMD? Most Windows Operating System users are aware of this line interpreter, but some of the users ignore its functions.
Well, the CMD accepts various commands from the user via the keyboard and process those commands to give the desired result.
However, since most of the Windows users are already familiar with buttons and other Windows features that make operating the OS easier, they are not comfortable with issuing commands to their OS via the Command Prompt.
Nevertheless, you can still use the Command Prompt for any purpose you want by creating one with the right command to be executed by the Command Line Interpreter (CLI). How do you do that?
How To Generate Batch File In Windows 10?
A batch file allows you to write all your commands in a Notepad++ or a simple Notepad and saves the commands to be executed as a batch.
To make the text function as a batch, save the file with .cmd, .bat, or .btm extension. The command will then be executed in the CMD.
Follow this step-by-step guide to create your first batch file and run it on your Command Prompt:
- Open a Notepad++ or a Notepad and type ipconfig/flushdns.
- Save the file with any name of your choice with the .bat extension, e. g. myfile.bat rather than the regular .txt extension.
Note that the file will suggest the .txt extension since that is the default extension for such files. Rather than choose the default extension, choose the .bat extension.
If you can’t find the extension, it is probably hidden in the Hide File Extension in the control panel. You should turn off the feature to get access to the extension.
To turn it off, do the following:
- Go to the Control Panel.
- Choose File Explorer Options.
- Click the View tab. You will see “Hide extensions for known file types.”
- Uncheck the feature. If you don’t Turn OFF the feature, you won’t be allowed to change the extension to .bat.
3. When you are through with this, you can run the file by simply double-clicking the batch file wherever you saved it.
This was the output of the batch file when I double-clicked it.
Alternatively, you can run the file directly from the Command Prompt.
To do this, open the file through the Command Prompt by navigating to the folder/delivery that the folder is saved. Then, type the file name together with the extension to execute it.
For instance, to run myfile.bat, I’ll type myfile.bat at the Command Prompt and press the “Enter” key.
You can see the relative ease you can create and run a batch file. However, it is imperative that you understand some terms used in the command to make it easier for you to create batch files for any purpose.
Can you notice the difference in the output when I changed the first line from “echo off” to “echo on” note that I run the same lines of command and the only difference is the “off” and “on” in the command?
- The ‘echo off’ command serves the primary role of removing the command from being printed to the CMD, although the results will be printed.
- Preceding the “echo off” command with “@” will remove the line from the output completely. In addition, the Command Prompt will pause until you press any key before it becomes active again.
Various Commands To Use In Batch File
Here is a list of some of the basic commands you can use with your batch file commands:
- Echo: this command allows texts to be displayed.
- Echo Off: the command completes hides the texts while it displays both the prompt and the result of the command.
- DEL: This command deletes files.
- REM: The REM command is used to make comments to programs.
- MKDIR: You can create directories with this command.
- START: With this command, you can use a file’s default application to run it.
- RMDIR: While the MKDIR command can be used for creating directories, this command can be used for removing directories.
- FOR/IN/DO: Use this command for specifying files.
- XCOPY: Used for copying files.
- TITLE: The command is used for editing the window’s title.
- @ECHO OFF: For hiding both the prompt and texts. Only the result of the execution will be displayed.
- EXIT: When you are through with your operations and want to exit the Command Prompt, use this command.
- PAUSE: Stop a batch file from execution with this simple command.
- CLS: The command clears everything from the Command Prompt screen.
There are tons of things you can do with a batch file. Let me give you an example of what you can do with this file.
Check For Network Connection
This simple batch file code will check your system or network connection:
:: A batch file that checks for possible network connection problems
:: Display details of network connection
:: Check if you can reach Google.com
:: Check the Google.com route by running a traceroute code
If you type this code in a Notepad and save it as a batch file, it will check your computer system for network connection when you run it using either of the two methods discussed above.
Note: The PAUSE command performs a specific function that is integral to the overall performance of the code. Without this command, the batch file will close as soon as it executes the command.
In this code, it will simply display the network connection details and close the window immediately. If you want to run a command while you don’t care about the output, you can omit this command.
As you can see, the batch file is a powerful file that can be used for a variety of purposes. The good news is that it is easy to create.
Take advantage of this file to increase your knowledge of the Command Prompt and have more control over your personal computer.
You can see a lot of sample batch files on the Internet. Find these codes and learn how you can maximize their potentials better.
So, you have learned, how to create a batch file in Windows 10, right? Glad, I could help you!