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
Sed Command in Linux/Unix With Examples The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
How to Find the Wi-Fi Password Using CMD in Windows Forgotten your Wi-Fi password? Need to connect a new device but canât recall the complex string of characters? You donât have to scramble for that sticky note or reset your router just yet! Hidden within Windows is a powerful, built-in tool that lets you retrieve your Wi-Fi password quickly and secu
8 min read
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
How to Download and Install the Google Play Store The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed?In this step-by-step guide, youâll learn how to safely download and install
6 min read
How to Change Directories in Command Prompt (CMD) The Command Prompt (CMD) in Windows is a powerful tool for executing commands, managing files, and navigating your system. One of the most common tasks youâll perform in CMD is changing directories, which allows you to move between folders to access and manage files. But have you know how to change
11 min read
Create virtual environment in Python A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. Using virtual environments is a common practice in Python development as it helps to manage dependencies for different projects, avoiding
3 min read
'crontab' in Linux with Examples If you do manually backups , update logs, or restart services on your Linux machine? Imagine that running repetitive tasks overnight so your machine works for you while you rest. Here crontab, the native job scheduler in Linux, which enables users to easily automate commands, scripts, and system tas
9 min read
How to Clear DNS Cache in Chrome using chrome://net-internals/#dns Command If you're experiencing website loading issues, seeing outdated pages, or encountering network errors in Google Chrome, clearing your DNS cache (also known as host cache) might be the solution. This method is useful if you are facing problems like slow browsing speeds, site access errors, or incorrec
5 min read
ls Command in Linux The ls command is one of the most used commands in the Linux terminal to display the files and directories or path in the terminal. So, using the ls command is a basic skill for navigating the Linux file system, handling files, and managing directories.What is the ls Command in LinuxThe ls command i
10 min read
How to List all Files in a Directory using CMD in Windows 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