Writing a Windows batch script
Last Updated :
29 Sep, 2022
In Windows, the batch file is a file that stores commands in a serial order. The command line interpreter takes the file as an input and executes in the same order. A batch file is simply a text file saved with the .bat file extension. It can be written using Notepad or any other text editor. A simple batch file will be:
// When echo is turned off, the command prompt doesn't appear in the Command Prompt window.
ECHO OFF
// The following command writes GeeksforGeeks to the console.
ECHO GeeksforGeeks
// The following command suspends the processing of a batch program and displays the prompt.
PAUSE
After saving it with a .bat extension. Double click it to run the file. It prints shows:
In the above script, ECHO off cleans up the console by hiding the commands from being printed at the prompt, ECHO prints the text “GeeksforGeeks” to the screen, and then waits for the user to press a key so the program can be ceased. Some basic commands of batch file:
- ECHO – Prints out the input string. It can be ON or OFF, for ECHO to turn the echoing feature on or off. If ECHO is ON, the command prompt will display the command it is executing.
- CLS – Clears the command prompt screen.
- TITLE – Changes the title text displayed on top of prompt window.
- EXIT – To exit the Command Prompt.
- PAUSE – Used to stop the execution of a Windows batch file.
- :: – Add a comment in the batch file.
- COPY - Copy a file or files.
Types of "batch" files in Windows:
- INI (*.ini) - Initialization file. These set the default variables in the system and programs.
- CFG (*.cfg) - These are the configuration files.
- SYS (*.sys) - System files, can sometimes be edited, mostly compiled machine code in new versions.
- COM (*.com) - Command files. These are the executable files for all the DOS commands. In early versions there was a separate file for each command. Now, most are inside COMMAND.COM.
- CMD (*.cmd) - These were the batch files used in NT operating systems.
Lets take another example, Suppose we need to list down all the files/directory names inside a particular directory and save it to a text file, so batch script for it will be,
@ECHO OFF
// A comment line can be added to the batch file with the REM command.
REM This is a comment line.
REM Listing all the files in the directory Program files
DIR"C:\Program Files" > C:\geeks_list.txt
ECHO "Done!"
Now when we run this batch script, it will create a file name geeks_list.txt in your C:\ directory, displaying all the files/folder names in C:\Program Files. Another useful batch script that can be written to diagnose your network and check performance of it:
// This batch file checks for network connection problems.
ECHO OFF
// View network connection details
IPCONFIG /all
// Check if geeksforgeeks.com is reachable
PING geeksforgeeks.com
// Run a traceroute to check the route to geeksforgeeks.com
TRACERT geeksforgeeks.com
PAUSE
This script displays:
This script gives information about the current network and some network packet information. ipconfig /all helps to view the network information and 'ping' & 'tracert' to get each packet info. Learn about ping and traceroute here.
Similar Reads
How to Create a Batch File in Windows? A batch file is a straightforward text document in any version of Windows with a set of instructions meant to be run by Windows' command-line interpreter (CMD) by maintaining all the functions. Batch files are a great tool for streamlining internally configured processes, automating tedious jobs, an
5 min read
How to Create an Infinite Loop in Windows Batch File? Windows batch files are a powerful tool for automating repetitive tasks, managing system operations, and streamlining workflows. But what happens when you want to create a process that runs indefinitely?In this blog post, we'll walk you through the steps to create an infinite loop in a Windows batch
4 min read
How to Create a File in CMD Creating a file using Windows GUI is very easy, but did you know that you can also create files using Windows CMD? Now, this article will walk you through the steps to create a file using CMD, ensuring that you can efficiently manage your files directly from the command prompt.How to Create a File i
4 min read
How to Create a Simple Shutdown Timer by using Notepad? In this article you will see how you can create your own shutdown timer using a notepad. Sometimes you need to schedule the shutdown time of your PC, so in such situations, this timer is very helpful. Let's see how you can create it. Shutdown Timer By Using Notepad Follow the below steps to implemen
1 min read
How to List all Files in a Directory using CMD Struggling to find or organize files buried in folders on your Windows PC? While File Explorer offers a visual way to browse, the Command Prompt (CMD) provides a faster, more powerful method to list, sort, and manage files, especially when dealing with large directories or automating tasks. In this
5 min read
How to Delete a File or Folder Using CMD? When working with files on Windows, File Explorer usually makes creating and deleting files simple. But what if File Explorer freezes or stops responding? In such cases, the Command Prompt (CMD) offers a reliable alternative. CMD allows you to create and delete files or folders using simple commands
4 min read
How to Create an Unnamed file in Windows? There is curiosity hidden inside everyone. So when it comes to computers we all try to know why this is happening, what will happen if I run this software, what will happen if I delete some of Windows files and etc. We also sometimes try to make a file without a name in Windows and fail and then, fi
2 min read
How to Clear the Windows Command Prompt Screen If you've been working in the Windows Command Prompt and your screen is cluttered with text, knowing how to clear the Windows Command Prompt screen can make your workspace much tidier and easier to manage. This simple task can help you focus on the commands you're working on without distraction. In
6 min read
How to Use the Xcopy Command in Windows? The xcopy command in Windows is a powerful tool for copying files and directories. It extends the functionality of the standard copy command by allowing the copy of entire directories along with their subdirectories and files all at once. This makes xcopy highly useful for tasks such as backing up f
4 min read
How to use CMD for Python in Windows 10? This article will guide you on how to run Python in CMD, execute scripts, and troubleshoot common issues. By mastering these CMD commands, you'll improve your coding efficiency and streamline your development process.Steps to Use CMD for Python in Windows 10Find the detailed steps to use CMD for Pyt
4 min read