Basics of Batch Script

In a batch script, you mostly use commands that also work in the Command Prompt. Some commands are basic, such as those for printing text, pausing the script, or closing the window, while others serve specific purposes, such as checking ping or viewing network statistics. Instead of opening the Command Prompt every time and typing the commands yourself, you can simply create a batch script file and run it whenever needed. There are many commands you can use in your batch scripts, but here are a few of the most basic:

ECHO – Displays text in the Command Prompt window.@ECHO OFF – Hides the command display and only shows messages on a clean line.TITLE – Changes the title of the Command Prompt window.PAUSE – Stops the Command Prompt window from closing automatically after the script finishes running.

Note: Make sure the name of your batch file is different from any default system files to avoid conflicts. You can also use the ‘.cmd’ extension; however, earlier versions of Windows may not support it.

Writing Simple Batch Scripts

To get started, you can write a simple batch script to understand how commands work. Just like in other programming languages, the first thing you often do is print text. Here, you’ll use the ECHO command to print a simple message. Follow these steps to create your first batch script file:

Writing Different Batch Scripts for Different Purposes

Below are a few examples to demonstrate how batch scripts work in different scenarios. The method for creating the script is the same as shown earlier—simply replace the code in Notepad with one of these examples, then save and run your batch file.

1. Copy or Move Files Using a Batch Script

You can use a batch script to copy files from one location to another. For example, this can be handy if you often need to copy or move photos from your phone or camera SD card to your computer. If you regularly use the same source (like a USB or SD card), this batch file saves you from manually selecting files every time. By setting the source and destination, you can simply run the script to copy or move files. Note: To move files instead of copying, change ‘xcopy‘ to ‘move‘ in your script.

2. Change the File Extensions in a Folder

You can create a batch file that changes the extensions of all files in a given folder. Extensions can be changed within similar file formats—for example, changing JPG files to PNG—or you can change the file type completely. If you wrote batch script code in a text file, you can change the file extension from .txt to .bat using this script:

3. Check Ping for Two Different Sites Using a Single Line Command in a Batch Script

This example shows how to use more than one command in a batch script. It’s helpful if you want to run a set of useful commands one after another to reach a certain goal. Here’s how you can check the ping of two different websites with one line: These are just a few of the many things you can do with batch scripts by following some basic rules and using different commands. Experiment with the above examples, and you’ll soon get the hang of automating tasks using batch files!

Batch Scripts on Windows 10  Making Life Easier - 40Batch Scripts on Windows 10  Making Life Easier - 8Batch Scripts on Windows 10  Making Life Easier - 8Batch Scripts on Windows 10  Making Life Easier - 6Batch Scripts on Windows 10  Making Life Easier - 92Batch Scripts on Windows 10  Making Life Easier - 80Batch Scripts on Windows 10  Making Life Easier - 62