Batch Scripts on Windows 10: Making Life Easier
Batch scripts are set of commands written in a file that is executed to automate tasks. The commands/code is executed one by one in sequence as they are written in different lines. These files are used to make it easier for users when using the commands in the command prompt. It is also time-saving if the commands exceed than just one or two.
Basics of Batch Script
In the batch script, you mostly write commands that can work in the command prompt. Some are basic commands for printing, pausing, exiting and some commands can be used for different purposes like checking ping, checking network stats and so on. Rather than opening a command prompt each time and typing the command by yourself, you can make the batch script file and just open it to work.
There are many commands that you can use in your batch scripts for different purposes, however, some basic commands are listed below:
- ECHO – Displays the text on the screen in the command prompt.
- @ECHO OFF – Hides the display text of command and only shows the message on a clean line.
- TITLE – Changes the title of the command prompt window.
- PAUSE – Stops the command prompt window from closing automatically after executing the command.
Note: The name of the file should be different from the default system files, so it does not conflict with each other and create a mess. You can also use the extension ‘.cmd’ the earlier versions of Windows won’t run it.
Writing Simple Batch Scripts
Users can try the simple batch script to understand the commands and working on it. Just like in other programming languages, you print text to understand the printing method; here we will be printing string by using the ECHO command. Follow the below steps to make your first batch script file:
- Hold the Windows key and press S to open the search function. Now type ‘notepad‘ and press Enter to open notepad.
- By following the above basic commands you can write the simple batch script as shown below:
@ECHO OFF :: This is a comment that you can write in batch script. Title APPUALS :: Title is the cmd window name. ECHO Hello Appuals Users, this is a simple batch script. PAUSE
- Click on File in the top menu bar and click on Save As. Rename the file and change the extension to ‘.bat‘ and click the Save button.
- Double-click the file to run the batch script file.
Writing Different Batch Scripts for Different Purposes
Some of the examples to show you the working of batch scripts through different scenarios. Each batch script below will have the same method for making, so we will use the above method for creating the batch script and add any of the below codes instead of the above code.
1. Copying/Moving Files by Using a Batch Script
A batch script for copying files from source to destination. This example can be used for copying or moving photos from your phone or camera SD card to your system folder. This batch file can be used if you mostly use the same source (USB/SD Card) for moving files. Users don’t need to select the new files in the USB each and every time they want to move/copy them on the PC. By defining the source and destination location, you can copy/move files by just clicking this batch script.
- Create the text file and add the following code in it:
xcopy "E:\New Folder\*.apk" "D:\My Folder\"
Note: The first path is to source and the second path is for the destination. To copy all the files from source path just remove the ‘.apk‘ extension and it will copy everything.
- Save the file with the extension ‘.bat‘ and run the file.
Note: You can also move files by changing the ‘xcopy‘ to ‘move‘ in the above code.
2. Changing the Files Extension in a Folder
You can also create a batch file for changing the extension of all files in the folder. Extensions can be changed to a similar file format, such as JPG to PNG or it can completely change the working of the file. If the text file has a code for batch script, the user can change the file extension from .txt to .bat as shown below:
- Make a text file and open it in notepad. Write the following code as shown below:
@ECHO OFF ren *.txt *.png
- Save it with the extension ‘.bat‘ and double-click the file to make it work.
3. Checking Ping for Two Different Sites by Using the Single Line Command in Batch Script
This is an example of using the multiple commands for command prompt through a batch script. This depends on the need of the user and their personal preference. There are some useful commands, which can be used one by one to achieve a specific purpose. Down below we have a code for checking the ping of two different URLs:
- Once you create a new text file, then write the following code in it:
@ECHO OFF TITLE CHECKING PING ping www.google.com && ping www.appuals.com PAUSE
Note: You can also write each command in a different line. However, ‘&&‘ in code is for the purpose where the second command will only be executed if the first command is executed without fail. The user can also use a single ‘&‘ where both commands will work even if one fails.
- Save it with the ‘.bat‘ extension and open it.
Note: You can add any URL that you would like to check ping for.
There is much more that users can do with batch scripts by following the batch script rules.