In this tutorial you will learn the basics of batch programming. It is important that you read all sections and don’t skip parts as they may contain important information.
IMPORTANT DISCLAIMER:
I am not responsible for any damages or trouble that happens due to using this guide (damages include computers/files/systems etc).
This guide is to be used AT OWN RISK.
I will explain every piece of code that is used in the guide please respect it.
This guide is copyright to Possum, appropriate action will be taken if this guide is found on any other sites without my permission.
This guide is not to be reproduced in anyway or form without the owners permission.
Contents
Part 1: Basic Programming using batch files.
1. What is a batch (bat) file?
2. What programs we’ll use
3. Begining
• The Basics and Pause Commands
• Msg * and cls Commands
• Rem and start Commands
• Goto, set and if Commands
• Call and exit commands
• Opening Applications using start Command
• Timeout Commands
• TIME Command
• Coloring Batch Words
• Title Command
• Shutdown and Restart Commands
Part 1: Basic Programming using batch files.
1. What is a batch (bat) file?
Before we can start programming a batch (otherwise known as a bat file) is a CMD command prompt, programmed or coded to do a specific task.
This task can range from opening MS paint or a picture in your documents, to creating a file that will open programs when commanded to, bats can also be used for hacking and/or viruses’, hacking is also done through DOS.
2. What programs we’ll use
We will be using notepad and command prompt.
Notepad is found:
Toolbar > Start > All Programs > Accessories > Notepad
Command Prompt is found:
Toolbar > Start > All Programs > Accessories > Command Prompt
3. Begining
The Basics and Pause Command
We’re going to start at the very beginning.
Here I’ll explain @echo on/off, echo and pause commands.
Open notepad and type in the below:
Now save this, click Save As.
Now when you get to the saving box, rename the file: test1.bat save this.
Now launch the file you just made.
A black box should open and it should say:
When you press a key the box will close.
Now, we don’t like all those directories;
So let’s remove them.
To do this go back to the file, right click it and select edit, click this. It will open notepad with the code in it.
Now let’s edit the code, type in this:
Now Save As a new file and rename it as test2.bat
Put this along side test1
Launch the program like before.
You should get the same black box but it should say:
Like before, when you press a key the box will close.
So for the explanation,
@echo off/on:
If you put @echo on it will show the directory as you just saw.
If you put @echo off it will just show the words with no directories.
So it’s best to use @echo off
echo:
If you want to type a simple phrase just type in echo and something, in this case it is; Hello world!
This then will show: Hello world!
pause
pause will pause the batch file, if you press a key the batch will continue in this case it will exit because there is no more code left to run.
Well done, you just created your first two batch files.
Now let’s keep moving.
Open notepad and type in:
Save this as test3.bat alongside the others.
Launch it and it will show in the black box:
So that covers the pause command.
Msg * and cls Commands
Now here are some more commands:
There is msg *
and there is cls
Open notepad and write:
And save as test4.bat
Launch it and it will show:
So for the explanation,
cls will just clear the screen and msg * will bring up a pop-up.
Rem and start Commands
Here are some more commands, they are rem and start
So create a folder name it Test put all the batch files you have made so far in it and put a picture inside, any picture, and name it testpic
Open notepad and type:
Save it inside the new folder as test5.bat
Launch it, it will show:
NOTE: if your picture didn’t open, it is most likely because it’s not a .jpg file.
To fix this you need to open the file with paint and then save it as a .jpg file.
So for the explanation,
start will launch any file and rem is a remark which wont show in the batch file when running but will in the code.
Goto, set and if Commands
Now for goto, set and if
Open notepad and type:
Save it and call it test6.bat, then launch it.
It will show:
So for the explanation,
goto will go to a label or a part of the file when you put a label, in this case the label is: :text and when you put goto :math the batch will go to :math
A new label will have to be named something different e.g. :begin
The set option will set something here its going to set a choice the set is, in some options followed by if.
So if I type 1 in this example the "choice" will be 1 and it will go to :text because we put:
if '%choice%'=='1' goto :text
So if we put 1, the choice will be 1 therefore it will goto :text
Call and exit commands
I am going to now mention the call and exit
Open notepad and type this:
And save renaming it test7.bat
Then open notepad again and type this:
And save in the same folder that you save the last one... and name it call.bat
Launch the first one, this will show:
Opening Applications using start Command
We will now learn how to launch windows applications:
Open notepad and type:
Save as notepad.bat and launch it.
This will show:
Timeout Command
Now let’s learn how to put a timeout inside a batch.
Open notepad and type:
If you don’t understand the command just copies paste it or memorize it, if I tell you the explanation it will take years.
Save this as timeout.bat and launch it.
So this will show:
TIME Command
Now I'll teach you how to change the time on the toolbar.
Please note: that this will change the time on the toolbar if you have admin privileges. You will manually need to change it back!
Open notepad and type:
Save it as time.bat and launch it.
If you don’t have admin privileges it will show:
Coloring Batch Words/text
Now for Coloring batch words and characters.
Open notepad and type this:
Save it as colortest.bat and launch it, see what happens!
So for the explanation,
04 = background black and font red
4f = background red and f white font
Here is the list of CMD colors:
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
Title Command
Now the title command.
Open notepad and type this:
Save it as title.bat and launch.
This will show the title of the batch being “Test” when you launch it.
Shutdown and Restart Commands
Let’s try Shutdown/Restart
(DO NOT RUN UNLESS YOU WANT TO)
Open Notepad and type this:
Save as restart.bat
This will restart the computer.
To shutdown the computer, open notepad and type:
Save as shutdown.bat
TUT End.
Hope this helped A LOT.
Any questions, post below. I'll do what i can to help you.
IMPORTANT DISCLAIMER:
I am not responsible for any damages or trouble that happens due to using this guide (damages include computers/files/systems etc).
This guide is to be used AT OWN RISK.
I will explain every piece of code that is used in the guide please respect it.
This guide is copyright to Possum, appropriate action will be taken if this guide is found on any other sites without my permission.
This guide is not to be reproduced in anyway or form without the owners permission.
Contents
Part 1: Basic Programming using batch files.
1. What is a batch (bat) file?
2. What programs we’ll use
3. Begining
• The Basics and Pause Commands
• Msg * and cls Commands
• Rem and start Commands
• Goto, set and if Commands
• Call and exit commands
• Opening Applications using start Command
• Timeout Commands
• TIME Command
• Coloring Batch Words
• Title Command
• Shutdown and Restart Commands
Part 1: Basic Programming using batch files.
1. What is a batch (bat) file?
Before we can start programming a batch (otherwise known as a bat file) is a CMD command prompt, programmed or coded to do a specific task.
This task can range from opening MS paint or a picture in your documents, to creating a file that will open programs when commanded to, bats can also be used for hacking and/or viruses’, hacking is also done through DOS.
2. What programs we’ll use
We will be using notepad and command prompt.
Notepad is found:
Toolbar > Start > All Programs > Accessories > Notepad
Command Prompt is found:
Toolbar > Start > All Programs > Accessories > Command Prompt
3. Begining
The Basics and Pause Command
We’re going to start at the very beginning.
Here I’ll explain @echo on/off, echo and pause commands.
Open notepad and type in the below:
Code:
@echo on
echo Hello world!
pause
Now save this, click Save As.
Now when you get to the saving box, rename the file: test1.bat save this.
Now launch the file you just made.
A black box should open and it should say:
Code:
c:\ Documents and Settings\your username\My Documents\echo Hello world!
c:\ Documents and Settings\your username\My Documents\pause
Press any key to continue…
When you press a key the box will close.
Now, we don’t like all those directories;
Code:
c:\ Documents and Settings\your username\My Documents\echo Hello world!
So let’s remove them.
To do this go back to the file, right click it and select edit, click this. It will open notepad with the code in it.
Now let’s edit the code, type in this:
Code:
@echo off
echo Hello world!
pause
Now Save As a new file and rename it as test2.bat
Put this along side test1
Launch the program like before.
You should get the same black box but it should say:
Code:
Hello world!
Press any key to continue…
Like before, when you press a key the box will close.
So for the explanation,
@echo off/on:
If you put @echo on it will show the directory as you just saw.
If you put @echo off it will just show the words with no directories.
So it’s best to use @echo off
echo:
If you want to type a simple phrase just type in echo and something, in this case it is; Hello world!
This then will show: Hello world!
pause
pause will pause the batch file, if you press a key the batch will continue in this case it will exit because there is no more code left to run.
Well done, you just created your first two batch files.
Now let’s keep moving.
Open notepad and type in:
Code:
@echo off
echo Hello world!
pause
echo I am testing pause
pause
Save this as test3.bat alongside the others.
Launch it and it will show in the black box:
Code:
Hello world!
Press Any Key To Continue...
(When you press a key it will show)
I am testing a pause
Press any key to continue…
So that covers the pause command.
Msg * and cls Commands
Now here are some more commands:
There is msg *
and there is cls
Open notepad and write:
Code:
@echo off
echo Hello world!
pause
cls
echo I am testing pause
msg * The End
pause
And save as test4.bat
Launch it and it will show:
Code:
Hello world!
Press Any Key To Continue...
(when you press a key it will show)
(the page will clear)
I am testing a pause
(a pop-up will show saying The End)
Press Any key to continue…
So for the explanation,
cls will just clear the screen and msg * will bring up a pop-up.
Rem and start Commands
Here are some more commands, they are rem and start
So create a folder name it Test put all the batch files you have made so far in it and put a picture inside, any picture, and name it testpic
Open notepad and type:
Code:
@echo off
echo Testing start and rem
rem title Test
start testpic.jpg
pause
Save it inside the new folder as test5.bat
Launch it, it will show:
Code:
Testing start and rem
Press any key to continue…
(and the picture will launch)
NOTE: if your picture didn’t open, it is most likely because it’s not a .jpg file.
To fix this you need to open the file with paint and then save it as a .jpg file.
So for the explanation,
start will launch any file and rem is a remark which wont show in the batch file when running but will in the code.
Goto, set and if Commands
Now for goto, set and if
Open notepad and type:
Code:
@echo off
echo This is a test
echo If you want to see text type 1 and press enter
echo If you want to see a picture type 2 and press enter
set /p option=
if '%option%'=='1' goto :text
if '%option%'=='2' start testpic.jpg
:text
echo Hello
pause
echo I love batchs
pause
Save it and call it test6.bat, then launch it.
It will show:
Code:
This is a test
If you want to see text type 1 and press enter
If you want to see a picture type 2 and press enter
(here you can type 1 or 2 and press enter)
(if you press 2 and Enter the picture will load)
(if you press 1 and enter this will show up):
Hello
Press any key to continue...
(when you press a key it will show)
I love Batchs
Press any key to continue...
(when you press a key it will close the batch)
So for the explanation,
goto will go to a label or a part of the file when you put a label, in this case the label is: :text and when you put goto :math the batch will go to :math
A new label will have to be named something different e.g. :begin
The set option will set something here its going to set a choice the set is, in some options followed by if.
So if I type 1 in this example the "choice" will be 1 and it will go to :text because we put:
if '%choice%'=='1' goto :text
So if we put 1, the choice will be 1 therefore it will goto :text
Call and exit commands
I am going to now mention the call and exit
Open notepad and type this:
Code:
@echo off
echo Yay I know the basics of batch.
call call.bat
pause
exit
And save renaming it test7.bat
Then open notepad again and type this:
Code:
@echo off
echo I love batchs
pause
And save in the same folder that you save the last one... and name it call.bat
Launch the first one, this will show:
Code:
Yay I know the basics of batch
I love batchs
Press Any Key To Continue....
(when you press a key the below will appear)
Press Any Key To Continue...
(and when you press a key it will exit the batch)
So for the explanation,
exit exits the file, simple.
call ,calls in another batch file.
Opening Applications using start Command
We will now learn how to launch windows applications:
Open notepad and type:
Code:
@echo off
echo I am testing notepad!
start /MIN notepad.exe
pause
Save as notepad.bat and launch it.
This will show:
Code:
I am testing notepad
(and notepad will start minimized, if you put /MAX instead of /MIN it will launch maximized but you can also leave /MAX and /MIN out and just have “start notepad.exe”)
Press Any Key to Continue…
Timeout Command
Now let’s learn how to put a timeout inside a batch.
Open notepad and type:
Code:
@echo off
echo I am testing timeout
set wait=0
:pause1
set /a wait=%wait%+1
if %wait% leq 1000 goto pause1
echo IT WORKED!
pause
If you don’t understand the command just copies paste it or memorize it, if I tell you the explanation it will take years.
Save this as timeout.bat and launch it.
So this will show:
Code:
I am trying timeout
(Couple of seconds and...)
IT WORKED!
Press Any Key To Continue...
TIME Command
Now I'll teach you how to change the time on the toolbar.
Please note: that this will change the time on the toolbar if you have admin privileges. You will manually need to change it back!
Open notepad and type:
Code:
@echo off
echo Gee!.. I wonder what time it is?
TIME 5:50
echo Oh so its 5:50
pause
Save it as time.bat and launch it.
Code:
This will show:
Gee!.. I wonder what time it is?
Oh so its 5:50
If you don’t have admin privileges it will show:
Code:
Gee!.. I wonder what time it is?
A required privilege is not held by this client
Oh so its 5:50
Coloring Batch Words/text
Now for Coloring batch words and characters.
Open notepad and type this:
Code:
@echo off
echo testing the colors
pause
color 04
echo testing
pause
color f
echo testing
pause
color 4f
echo testing
pause
echo testing complete.
pause
Save it as colortest.bat and launch it, see what happens!
So for the explanation,
04 = background black and font red
4f = background red and f white font
Here is the list of CMD colors:
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
Title Command
Now the title command.
Open notepad and type this:
Code:
@echo off
title Test
echo testing title
pause
Save it as title.bat and launch.
This will show the title of the batch being “Test” when you launch it.
Shutdown and Restart Commands
Let’s try Shutdown/Restart
(DO NOT RUN UNLESS YOU WANT TO)
Open Notepad and type this:
Code:
@echo off
START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec
exit
Save as restart.bat
This will restart the computer.
To shutdown the computer, open notepad and type:
Code:
@echo off
C:\Windows\RUNDLL32.EXE user,exitwindows
exit
Save as shutdown.bat
TUT End.
Hope this helped A LOT.
Any questions, post below. I'll do what i can to help you.