How to Write a Simple Batch (BAT) File
Batch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate.
- Open a text file, such as a Notepad or WordPad document.
- Add your commands, starting with @echo [off], followed by—each in a new line—title [title of your batch script], echo [first line], and pause.
- Save your file with the file extension .bat, for example, test.bat.
- To run your batch file, double click the BAT file you just created.
- To edit your batch file, right-click the BAT file and select Edit.
@echo: This parameter will allow you to view your working script in the command prompt. This parameter is useful for viewing your working code. If any issues arise from the batch file, you will be able to view the issues associated with your script using the echo function. Adding a following off to this parameter will allow you to quickly close your script after it has finished.
title: Providing much of the same function as a <title> tag in HTML, this will provide a title for your batch script in your Command Prompt window.
cls: Clears your command prompt, best used when extraneous code can make what you’re accessing had to find.
%%a: Each file in the folder.
(“.\”): The root folder. When using the command prompt, one must direct the prompt to a particular directory before changing a files name, deleting a file, and so on. With batch files, you only need to paste your .bat file into the directory of your choosing.
pause: Allows a break in the logical chain of your .bat file. This allows for users to read over command lines before proceeding with the code. The phrase “Press any key to continue…” will denote a pause.
start “” [website]: Will head to a website of your choice using your default web browser.
ipconfig: This is a classic command prompt parameter that releases information concerning network information. This information includes MAC addresses, IP addresses, and sub-net masks.
ping: Pings an IP address, sending data packets through server routes to gauge their location and latency (response time).
Sample1: It opens all the following websites. And cmd window will be closed immediately.
start "" http://www.cnn.com
start "" http://www.abc.com
start "" http://www.msnbc.com
title This is your first batch script!
echo Welcome to batch scripting!
*Pause make the cmd window opened.