See how to create a batch file (.BAT file) in the Windows operating system with simple steps.
What are batch files?
Windows batch files are a stack of simple commands that run on the Command Prompt (Command Prompt). When you open a Command Prompt window there you can give Windows various commands like copy, cls, echo etc.
All of these commands can be grouped into a .bat file and run in groups with just one click on the bat file, instead of typing one by one into one of the Command Prompt windows.
The batch file (batch file) so it is essentially an unformatted text file, since the commands in the Command Prompt are text. The extension of this file as we mentioned before is a .bat, but you can also see it as .cmd.
When users run a batch file, the Windows application opens the cmd.exe application, which undertakes to execute successively the commands contained within it, in the order in which they appear in the file. This way you can automate daily tasks and shorten the time required to do something.
How to create a batch file in Windows
A batch file is no different from an unformatted text file. And for this we will use the simplest word processor we have in our hands. The notebook!.
1. Open Notepad (notepad.exe). Alternatively you can open WordPad.
2. Write in the first row and as the first command @ Echo off
3. At the bottom line, start writing the commands to the Command prompt one below the other.
4. Save your file with any name you want and with a .bat extension
You can now double click on it and your file will run. To edit the batch file, right-click on the BAT file and select Processing.
Examples of files
Example 1: Network control
Suppose you often have network problems. Keep popping up at the command prompt by typing ipconfig and ping into Google for network troubleshooting. After a while, you will realize that it would be a little more effective if you wrote a simple BAT file, put it on your USB stick and used it on the machines you are having trouble with.
This file would look something like this:
Example 2: Tell Command Promt to write something
You want to make the Command prompt write your own phrase. Let's say the text "Welcome to iGuru!". Then your commands should be the following:
title Αυτή είναι μία δοκιμή!
echo Καλώς ήλθατε στο iGuru!
Example 3: Take a backup
Do you want to Backup to an external drive (D) specific folders on your computer (C)? You can of course use a third-party application or just make your own with two commands each and all. And then tell Windows Schedule to run it automatically every night (or whatever time you want).
Your file will look something like this,
xcopy "c:\fakelos pros antigrafi 1" "d:\fakelos backup 1" /E /H /Y /I /D /C /K /R /X /S
xcopy "c:\fakelos pros antigrafi 2" "d:\fakelos backup 2" /E /H /Y /I /D /C /K /R /X /S
xcopy "c:\fakelos pros antigrafi 3" "d:\fakelos backup 3" /E /H /Y /I /D /C /K /R /X /S
I have been using this example for years now for the daily backup of my files to an external drive.
Do not assume that this is the only thing a junkie can do. The variety and parameters of these commands are huge. For those unfamiliar, we inform you that the command line is capable of many.
Read: 20 Command Prompt commands worth knowing
Learn the basics of Batch Scripting
Batch files use the same language as the command line. All you have to do is tell the command line what you want it to do using a file, instead of typing it in the command line. This saves you time and effort. It also allows you to put some logic, such as simple loops, conditional actions, etc.
Clearly you can use all of the Command Prompt commands, but here are some basic ones:
- @ Echo: This parameter will allow you to see your work script in the command line. Basically this parameter is useful for viewing your work code. If problems arise from the batch file, you will be able to view issues related to your script using the echo function. Adding one off at the end of the command will allow you to quickly close your script once completed.
- title: Providing the same function as a title tag in HTML will provide a title for the batch script in the command prompt window.
- class: Clears your command prompt window.
- rem: Abbreviation of remark. Provides the same functionality as the label
<!--in HTML. Rem statements are not entered in your password. Instead, they are used to explain and give information about the code.
- %% a: Each file in a folder.
- (“. \”): The root folder. Used when you need to direct the command line to a specific directory.
- pause: Allows a break in the logical chain of the BAT file. This allows users to read the command lines before moving on to the code. After the pause you will see in the command prompt the phrase "Press any key to continue…".
- start “” [website]: You will be directed to a website of your choice using your default browser.
- ipconfig: This is a classic command line parameter that gives you information about your network. This information includes MAC addresses, IP addresses, and subnet masks.
- ping: Pings an IP address, sending data packets through server paths, to measure location and latency (response time).
- xcopy: Copies a folder or file to a new location. It accepts many parameters and that is why it is especially useful for backup.
The library for variables in a bacth file is, to say the least, huge. Fortunately there is one Wikibook article which has at your disposal the extensive library of parameters and script variables. It's worth having in your bookmarks.
Read: Write your first .bat file
Write and execute your BAT file
Let's create two examples of scripts that can simplify your daily activities.
Example 1: A look at the news
You woke up in the morning and at the first sip of coffee you want to see your favorite news sites for the news of the day. You can create a script that opens any media in a single browser window with a single click.
Let's look at the steps: first, create a blank text file. Right-click an empty space in a folder of your choice or on the desktop and select Create and then, Text document. With the text file open, type the following script. Our example will provide the mainstream American media available on the Internet. You put your own, Greek or foreign.
cd "" https://www.cnn.com
start "" https://www.abc.com
start "" https://www.msnbc.com
start "" https://www.bbc.com
start "" https://www.iguru.gr
start "" https://www.aljazeera.com
start "" https://news.google.com/
The above scenario stacks the start command on top of each other to open multiple tabs. Save your script from File> Save As. Save your file by entering a name and the .bat extension. Do not forget to change the "Save As" parameter to "All Files (*. *)" To allow you to save it as a .bat file. If you accidentally saved it as .txt just rename it to .bat.
After saving your file, all you have to do is double-click the BAT file. Immediately, your web pages will open. If you want, you can place this file on your desktop. This will allow you to access all of your favorite sites at the same time, especially when you're still eye-popping.
Example 2: Organize files
Did you download many files a day, so you have hundreds of files in the Download folder? Create a batch file with the following script, which sorts your files by file type. will create subfolders named after your file extensions and place each file in the corresponding subfolder. Place the BAT file in the disorganized folder and double-click to run.
rem Για κάθε αρχείο που υπάρχει εδώ, κάνε
for %%a in (".\*") do (
rem έλεγξε αν το αρχείο έχει extension
if "%%~xa" NEQ "" if "%%~dpxa" NEQ "%~dpx0" (
rem έλεγξε αν υπάρχει φάκελος με το όνομα του extension, και αν δεν υπάρχει δημιούργησέ τον
if not exist "%%~xa" mkdir "%%~xa"
rem μετακίνησε το αρχείο στον αντίστοιχο φάκελο
move "%%a" "%%~dpa%%~xa\"
It's very simple. This batch script will also work with any type of file, be it a document, video or audio file. Even if your computer does not support the file format, the script will create a folder with the appropriate label for you. If you already have a JPG or PNG folder in your directory, the script will simply move your file types to the appropriate location.
Automate simple things with scripts
This is just a taste of what scripts offer in a batch file. The logic is that if you need to do something simple over and over again, be it ordering files, opening multiple web pages, bulk renaming, backing up important documents, etc., you can do the tedious task with a simple Command Prompt script.
If you are also using a batch file for a recurring task, we will be happy to share it with you in the comments section of this article.