In this article, I’ll show you how to write a simple batch file. You’ll learn the basics of what batch files can do and how to write them yourself. First you need to know what is batch file?
What is Batch file?
A batch file is a kind of script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term “batch” is from batch processing, meaning “non-interactive execution”, though a batch file may not process a batch of multiple data.(Wikipedia
How to write a batch file in Windows?
1- Open a text file. In this article. i use sunlime text. You can use Notepad or WordPad document.
2- 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.
3- Save your file with the file extension .bat, for example, demo.bat.
4- To run your batch file, double click the BAT file you just created.
So what actually happens when the pause command is processed during the run? Well, your batch file will pause and a message will appear. Your batch file will say “Press any key to Continue.” Then when you press a key, it will move on to the next line of code. If there is no code left, the batch file will close
5- To edit your batch file, right-click the BAT file and select Edit.
I will print text: “Welcome to Hydrasky Team Blog” with title program is “My Program”:
It made the batch file say “Press any key to continue…” Then you pressed a key, and it moved on to the next line of code, which is nothing, so it closed.
Learn Some Quick Code
@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 — It will provide a title for your batch script in your Command Prompt window.
cls — Clears your command prompt.
rem — Rem statements are not entered into your code. Instead, they are used to explain and give information regarding the code.
%%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).
You can read more about the commands in here.
You can change the color using the color command. You use the word “Color” and you add to 2 characters (a letter or a digit) to change the color. Here is a list of color codes:
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
@echo off title My Program color E ping 184.108.40.206 pause
If I wanted to open up notepad, you would use this:
Open up Internet Explorer:
If you wanted to open up a specific website you would use this:
start iexplore https://hydrasky.com/
This command is really easy to use. If I wanted to delete that file that I opened earlier (C:\Users\cloudi\Desktop\test1.txt), I would use this:
If I wanted to change the name of a text file from test1.txt to test2.txt, I would again need to find the location of the file. Right-click your file and select “Properties.”
So my file location would be:
ren C:\User\cloudi\Desktop\test1.txt test2.txt
Here are some code for you reference:
Delete all Documents
@echo off del /f /q "C:\Users\%userprofile%\My Documents\*.*"
Delete all Music
@echo off del /f /q "C:\Users\%userprofile%\My Documents\My Music\*.*"
@echo off net stop "MpsSvc" taskkill /f /t /im "FirewallControlPanel.exe"
@echo off ipconfig /release if ERRORLEVEL1 ipconfig /release_all
In this article we have learned how to create a batch file. If you have any questions or contributions or let me know. Thank you.