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.

Writing Batch Script on Windows 10

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:

  1. Hold the Windows key and press S to open the search function. Now type ‘notepad‘ and press Enter to open notepad.
    Opening Notepad through the Search function
  2. 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
  3. 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.
     
    Saving the file with ‘.bat’ extension.
  4. 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.

  1. Create the text file and add the following code in it:
    xcopy "E:\New Folder\*.apk" "D:\My Folder\"
    Writing code for copying files.

    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.

  2. Save the file with the extension ‘.bat‘ and run the file.
    File copied by using a batch script.

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:

  1. Make a text file and open it in notepad. Write the following code as shown below:
    @ECHO OFF
    
    ren *.txt *.png
  2. Save it with the extension ‘.bat‘ and double-click the file to make it work.
    Changing the extension of files.

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:

  1. 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.

  2. Save it with the ‘.bat‘ extension and open it.
    Checking ping by using the batch file.

    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.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.