In Windows, the batch file is a file that stores commands in a serial order. 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
ECHO OFF ECHO GeeksforGeeks PAUSE
After saving it with .bat extension. Double click it to run the file. It prints shows
In 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 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 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 for 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.
- CDM (*.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 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.
- Windows 10 –Feel the Difference
- Useful CMD commands for daily use in Windows OS
- Reading and Writing data to excel file using Apache POI
- Wi-Fi Password of All Connected Networks in Windows/Linux
- Guide to Install MongoDB with Python | Windows
- Creating Python Virtual Environment in Windows and Linux
- IDE for Python programming on Windows
- Setup GitLab Repository On Windows 10
- How To Use Bash Shell Natively On Windows 10
- Basics of NS2 and Otcl/tcl script
- How Content Writing at GeeksforGeeks works?
- C++, OpenCV and Gtk3 Windows dev environment
- Windows Server 2012 R2 Stuck at "Updating Your System”
- Introduction to AWS Batch
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh