Your definitive guide to batch scripting 10-28-2011, 12:21 PM
#1
Your definitive guide to batch scripting
Simple programming commands in a batch environment.
Things you need to know about batch :
Things you need :
Lets us start learning this awesome language!
How to write a batch file :
Type in the DOS commands you want to execute, save it as a .bat file, and you are good to go!
However, there are also more sophisticated batch file structures, using simple programming commands built into the batch structure.
Your first batch file:
Most programming tutorials start with “Hello worldâ€Â, so I shall do the same
Type this code in notepad and save it as Hello world.bat.
The MS-DOS window then prints out :
So, how does this program work?
So, you may not want to have the “Press any key to continue...†at the end of your batch file, so now, we will learn the “nul†command.
Nul can be used in many different ways, like "pause > nul", or echo abc > nul
This command basically hides “Press any key to continue . . .â€Â, and can be used to hide any command. Let us make our first program with the nul command.
Type this code into Notepad and save it as [Your file name here].bat. Do you understand what this batch script does? The batch script will print “And this line too†2 lines below the first line because I used the command “echo.â€Â
When you want to enter an extra line when printing text on screen, you will type "echo.". It will show as a blank line in the ms - dos window. Well,what about the “::� This is used to type comments, like using // in C++. You can also use the REM command for comments. It is
used by programmers to insert comments into their code to make it more readable and understandable. It is normally not used in small easy to understand batch programs but is very useful in huge snippets of code with geek stuff loaded into it, which is really hard to understand.
Usage:
Launching programs and websites
Next, I will teach you how to launch programs from batch files. For most programs, you have to add the file path to run the file, but for windows built – in programs, you don’t! For example, you can type in :
and notepad will open
Some of the built – in programs ( you can find more online)
Now, you will learn how to launch websites with batch files.
Type in :
This will make the website will open in your default browser.
If you want to launch it in Internet Explorer (I hate IE, so I don't use it), type :
Let’s say you wanted to open a certain file in [My Documents] folder with a batch file. You can just type in :
It's that simple!
Changing the text & background colour
You will now learn how to change the colour (I am not going to use American English here
) of a batch file.
Scenario : You’re bored with the default colour of the window and text, but you don’t know how to change it. Well, let me teach you how!
The [color] ( american english
) command specifies colour attribute of console output.
Colour attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
Example: "color fc" produces light red on bright white
Note : You cannot have the same colour background and foreground. Type "color" and not "colour" :dance:
Changing the title of your DOS window : using the command titled “titleâ€Â
For example :
Copying files :
The “copy†command copies files from one location to another. The destination defaults to the current directory. So, your first input will be
Usage : copy “[destination of file + filename] †“[destination where you want to copy the files to]
For example, you want to copy a file from Your C: drive to your desktop. Here is how you will write the batch file.
Creating text files:
This batch file would add "Your text goes here" to the file qwerty.txt. If there is already text in the files, it will be erased and replaced by “Your text goes here†unless it is an empty file or you use†Your text goes here >> qwerty.txt†. This will keep the text in the file and add a new line.
Loops
Now, we will learn about loops and how to execute them. Loops save A LOT of time. For example, I can create a batch file that pings "www.hackcommunity.com" over and over.
The goto command tells the command interpreter to goto a line with : (labelled line). Loops are definitely useful, but they can also be dangerous. For example, we can create a batch file that opens internet explorer infinitely until the computer crashes due to overload of RAM. I recommend you not to try this on your computer. :epic:
Tasklist & taskkill:
Next, we will learn the tasklist and taskkill commands. The tasklist shows the lists of running processes and taskkill removes them.
As you can see all the process running are shown. Now for the taskkill command.
We can use taskkill in many different ways like:
This kills the process with the process ID 1234.
You can also kill processes like this :
This kills notepad. This is useful in many ways, and can be used to kill lagging processes.
Assigning variables:
Now, we will move on to assigning variables. In programming, a variable is a set of number, letters or symbols used to assign values to anything . This is useful because most programs wait for user input before doing what the user inputted. To modify variables in batch, you will use the set command. And here’s the really cool part, You can make them up as you go along, and assign them as you wish (as long as you don’t use one that has a legitimate assigned value, such as, say, %appdata%, or like %windir%.
The batch file will print out “Which country are you from?†and them sets the variable “userInput†Set/p tells the interpreter to wait for a user input. You can replace “userInput†with anything you want because that is the variable. Another batch file with variables :
This sets the variable abc to “Good day Hackcommunity!†and then prints the variable out.
The del command: Used to deleted files and folders.
This batch file goes to your C: drive and deletes the text file “abcâ€Â. I don't think I have to explain this.
Date and time in batch:
The %DATE% and %TIME% variables are already built – in commands, so they do not have to be assigned.
This is all for now, I will be updating this thread regularly.
Please Rate, comment and + rep if you liked this tutorial!
Have fun with batch scripting! :bye:
Simple programming commands in a batch environment.
Things you need to know about batch :
Quote:Batch is easy to learn and is fun to use.
Batch is native to windows and windows ONLY.
Batch is a truly awesome scripting language, but there are limits to what you can do with batch.
Batch is really useful, but it will never be as powerful as languages like C and C++.
Things you need :
Quote:Computer :epic:
Notepad or Notepad++
This tutorial
Brain.exe
Fingers.exe
Commonsense.dll
Lets us start learning this awesome language!
How to write a batch file :
Type in the DOS commands you want to execute, save it as a .bat file, and you are good to go!
However, there are also more sophisticated batch file structures, using simple programming commands built into the batch structure.
Your first batch file:
Most programming tutorials start with “Hello worldâ€Â, so I shall do the same
Type this code in notepad and save it as Hello world.bat.
Quote:Code:
@echo off
echo. Hello world!
pause
The MS-DOS window then prints out :
Quote:Hello world!
Press any key to continue...
So, how does this program work?
Quote:@echo off : When echo is turned off, the command prompt does not appear in the Command Prompt window. Used in most batch files. If you take away the echo off, all commands will be shown instead.
echo: Writes the text to the command interpreter
pause: Waits for user input (press any key) and pauses the batch file so that your program doesn’t exit once it’ s opened.
So, you may not want to have the “Press any key to continue...†at the end of your batch file, so now, we will learn the “nul†command.
Nul can be used in many different ways, like "pause > nul", or echo abc > nul
This command basically hides “Press any key to continue . . .â€Â, and can be used to hide any command. Let us make our first program with the nul command.
Quote:Code:
@echo off
echo You will not be able to see this line > nul
echo Nor will you be able to see this line > nul
::Neither will you be able to see this one!
echo You will be able to see this line,
echo.
echo and this line too.
pause >nul
Type this code into Notepad and save it as [Your file name here].bat. Do you understand what this batch script does? The batch script will print “And this line too†2 lines below the first line because I used the command “echo.â€Â
When you want to enter an extra line when printing text on screen, you will type "echo.". It will show as a blank line in the ms - dos window. Well,what about the “::� This is used to type comments, like using // in C++. You can also use the REM command for comments. It is
used by programmers to insert comments into their code to make it more readable and understandable. It is normally not used in small easy to understand batch programs but is very useful in huge snippets of code with geek stuff loaded into it, which is really hard to understand.
Usage:
Quote:@echo off
Rem Using this batch file, we are going to open the telnet client.
Rem You have to manually enable telnet on windows 7.
telnet
Launching programs and websites
Next, I will teach you how to launch programs from batch files. For most programs, you have to add the file path to run the file, but for windows built – in programs, you don’t! For example, you can type in :
Quote:start notepad.exe
and notepad will open
Some of the built – in programs ( you can find more online)
Quote:start mspaint
start msinfo
start calc
start msconfig
start regedit
start wmplayer
And many more!
Now, you will learn how to launch websites with batch files.
Type in :
Quote:start www.hackcommunity.com
This will make the website will open in your default browser.
If you want to launch it in Internet Explorer (I hate IE, so I don't use it), type :
Quote:start iexplore www.hackcommunity.com
Let’s say you wanted to open a certain file in [My Documents] folder with a batch file. You can just type in :
Quote:start C:\Users\[your login name]\Documents\[filename].[filetype].
It's that simple!
Changing the text & background colour
You will now learn how to change the colour (I am not going to use American English here

Scenario : You’re bored with the default colour of the window and text, but you don’t know how to change it. Well, let me teach you how!
The [color] ( american english

Colour attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
Quote: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
Example: "color fc" produces light red on bright white
Note : You cannot have the same colour background and foreground. Type "color" and not "colour" :dance:
Changing the title of your DOS window : using the command titled “titleâ€Â
Quote:Code : Title [your title]
For example :
Quote:Code:
@echo off
color f1
title Learning batch
echo Having fun learning batch so far?
pause > nul
Copying files :
The “copy†command copies files from one location to another. The destination defaults to the current directory. So, your first input will be
Usage : copy “[destination of file + filename] †“[destination where you want to copy the files to]
For example, you want to copy a file from Your C: drive to your desktop. Here is how you will write the batch file.
Quote:@echo off
copy "C:\[Your folder]\[Yourfilename]" "C:\Users\[Your username]\Desktop"
Creating text files:
Quote:@echo off
echo Your text goes here > qwerty.txt
This batch file would add "Your text goes here" to the file qwerty.txt. If there is already text in the files, it will be erased and replaced by “Your text goes here†unless it is an empty file or you use†Your text goes here >> qwerty.txt†. This will keep the text in the file and add a new line.
Loops
Now, we will learn about loops and how to execute them. Loops save A LOT of time. For example, I can create a batch file that pings "www.hackcommunity.com" over and over.
Quote:@echo off
:loop :: You can call the loop anything you want
ping www.hackcommunity.com
goto loop
The goto command tells the command interpreter to goto a line with : (labelled line). Loops are definitely useful, but they can also be dangerous. For example, we can create a batch file that opens internet explorer infinitely until the computer crashes due to overload of RAM. I recommend you not to try this on your computer. :epic:
Quote:@echo off
pam
Start iexplore
goto spam
Tasklist & taskkill:
Next, we will learn the tasklist and taskkill commands. The tasklist shows the lists of running processes and taskkill removes them.
Quote:
@echo off
Tasklist
Pause > NUL
As you can see all the process running are shown. Now for the taskkill command.
We can use taskkill in many different ways like:
Quote:@echo off
Taskkill /PID 1234
This kills the process with the process ID 1234.
You can also kill processes like this :
Quote:@echo off
Taskkill /IM notepad.exe
This kills notepad. This is useful in many ways, and can be used to kill lagging processes.
Assigning variables:
Now, we will move on to assigning variables. In programming, a variable is a set of number, letters or symbols used to assign values to anything . This is useful because most programs wait for user input before doing what the user inputted. To modify variables in batch, you will use the set command. And here’s the really cool part, You can make them up as you go along, and assign them as you wish (as long as you don’t use one that has a legitimate assigned value, such as, say, %appdata%, or like %windir%.
Quote:@echo off
echo Which country are you from?
set /p userInput=
echo %input%
pause
The batch file will print out “Which country are you from?†and them sets the variable “userInput†Set/p tells the interpreter to wait for a user input. You can replace “userInput†with anything you want because that is the variable. Another batch file with variables :
Quote:@echo off
set abc =Good day Hackcommunity!.
echo %abc%
pause
This sets the variable abc to “Good day Hackcommunity!†and then prints the variable out.
The del command: Used to deleted files and folders.
Quote:@echo off
Cd C:/
Del abc.txt
This batch file goes to your C: drive and deletes the text file “abcâ€Â. I don't think I have to explain this.

Date and time in batch:
Quote:@echo off
echo The date is %DATE%.
echo.
echo The time is %TIME%.
pause
The %DATE% and %TIME% variables are already built – in commands, so they do not have to be assigned.
This is all for now, I will be updating this thread regularly.
Please Rate, comment and + rep if you liked this tutorial!
Have fun with batch scripting! :bye:
(This post was last modified: 10-31-2011, 03:34 PM by thelastone.)