Introduction: VBS Tutorial - Basics

Picture of VBS Tutorial - Basics

Ok so some people have been asking me where did i learn my vbs from .. i learned from various websites, like the w3schools , even know that is for web scripting you can still use it from plain vbs.

I learn it from websites you'll learn it from me =D

So lets get started ...

What is vbs?
VBScript is a Microsoft scripting language. -_- so being Microsoft they go and make it so it only works on IE. But as well as being a web script it is used for all kinds of things ...
for example on pic 2) (that's the second pic) That is take from the System 32 folder. Vbs can also be used to make programs .. but that's VB.NET .. uses most of the same coding though.

Please enjoy the tutorial .. and please comment and rate.
And please tell me if someone has beet me to making a vbs tutorial .. but i couldn't find one.
Other than cammel8 who seems to be really good with vbs scripting =P .. but i'm still gonna make the tutorial.

Step 1: Basics of Vbs

Picture of Basics of Vbs

Ok so here are the basics .. stuff you should already know ...

You save the files as: something.vbs
It's not like a batch file it doesn't have a screen telling you information.
In a way it's much like javascript. But at the same time it's nothing like it.

to make a var you use dim
e.g .. dim iRule
now you would have a variable call iRule

now you can start to add things to you vars .. like:
iRule=msgbox("hello")
this would make a message box pop up saying hello. (pic 1)

Along with that you can add different buttons to the message box ..
here is all about message boxes: HERE
so many tutorials on them that i won't even bother to go into them.

Also with vars you can dim vars in an array:
dim iRule(3)
but that would turn out like this: (because 0 is included)

iRule(0)="var1"
iRule(1)="var2"
iRule(2)="var3"
iRule(3)="var4"

Using vars in the script..

You can use vars easily ..
you could have: (pic 2)


dim iRule
dim instructables

iRule="instructables"

instructables=msgbox("hello " & iRule)

Because of the & it says 'hello instructables' becuse the value of iRule is instructables.

Subs

You can also have subs:
A sub is a procedure that does NOT give a return value.

Sub iRule(arg1,arg2,arg3)
...Script...
End Sub

The arg 1,2,3 are the Arguments.

That about all of the basics .. enjoy them .. or keep reading for not so basics..

Step 2: Not So Basics of Vbs

Picture of Not So Basics of Vbs

I'm going to start this step with Functions... because i always find them annoying .. even though they are quite easy =P

functions in vbs are easy.. ish .. they can get confusing..
Lets start with an easy function: (pic 1)


Function times(x,y)
times = x * y
End Function

Dim result
dim var1

result = times(10,10)

var1=msgbox(result)


This would give you 100
let me explain ....
you told it to times 10 by 10..
result = time(10,10)
this went to the function times

x is now 10 and y is now 10
so: x * y return value with answer.



For, Next, Do, Loop

The For , Next loop can be used to repeat things, for example:


for var = 0 to 5
msgbox(var)
next
msgbox("Finish")


This will pop up a message box counting 0,1,2,3,4,5 then it will say 'Finish'
REMEMBER in vbs 0 nearly always counts!
so that code would repeat a command 6 times e.g:

for var = 0 to 5
msgbox("hello")
next
msgbox("Finish")

the message 'hello' would come up 6 times. on the 7th time it will say Finish.

If you add: step ... to the end of for var = 0 to 5 e.g
for var = 0 to 5 step 5
that will make it jump 5 each time.. in this case the message will only show twice because 5 is the limit.
You can also step down as well .. e.g. step -5 would count down 5 each time.

Do, Loop
the do loop is used to loop a piece of code over and over and over ect. mainly used for viruses =P

but you can use them to help you .. say if you wanted to keep saying a message until a certain option is picked. You can always add a Until on the do or on the loop part. e.g.
do until var=5
but in the code you must make it add 1 or more to the var .. or it will keep on looping.

You can also use do from thing like: do while var=10
this will only do the commands if var is equal to 10!

That's all for the Not So Basics of vbs.

Step 3: The If's and Then's

Theses are quite easy to get but i decided they needed a page in case someone didn't know what they did.
But really they are very easy : e.g

if instructables=TheBest then msgbox ("yes it is!")

But that is not hard as we all know that instructables is the best. =P
...now for multi-lined ifs and thens ... (scary music)

but there not that scary you just add a end if at the end of the is statement. e.g

if instructables=TheBest then
msgbox ("yes it is!")
msgbox ("really it is!")
end if

this will pop up with 2 messages one after the other, 'yes it is!' and 'really it is!'
the the end if statement closes it.



the else and elseif
These are not hard either ..

the else is just for when you want one option for one thing and another for the rest.. e.g

if var=1 then
msgbox ("var is 1")
else
msgbox ("var is not 1")
end if

so if var doesn't = 1 it will always say 'var is not 1'

the elseif is also very similar ...e.g

if var=1 then
msgbox ("var is 1")

elseif var=2 then
msgbox ("var is 2 ")

else
msgbox ("var is not 1 or 2")
end if

this would make it so if var was 1 or 2 it would say var is 1/2 ... but if it's not then it will say var is not 1 or 2.

Step 4: Case's

cases are simple and can make you life much easier .. e.g of simple case:


Dim FavCol
FavCol = "red"
Select Case FavCol
Case "Black"
msgbox("your Fav Colour is Black")
Case "red"
msgbox("your Fav Colour is Red")
Case "Yellow"
msgbox("your Fav Colour is Yellow")
Case Else
msgbox("Now your just confusing")
End Select

this simple script will select options from a list in this case it will tell you your fav colour is red.

Let me go into it in a bit more detail ...

you get your var : Dim var, var="iRule" var can quel anything
then you ask the vbs to look through a list to find your var ...
if it cannot find it it will go to the: Case Else which is just like the if, else command.
if it finds your var it will execute

Step 5: Passing Vars

Picture of Passing Vars

If you look around on the internet you'll find that lots of peopl want to know how to pass vars between batch to vbs and vbs to batch...
I'll show you the best way i found:

Since this is a vbs tutorial I'll show you how to transfer vars from vbs to batch first ...

VBS TO BATCH

This is the vbs:


dim a

a=InputBox("Type in somthing:","Var")

dim WshShell
set WshShell=Wscript.Createobject("Wscript.shell")
wshshell.run "test.bat " & a

This will ask you to type in a var then it will call test.bat passing the var you typed in.
a = what you typed in.

here is the batch:

@echo off

echo %1

pause
exit

this will write the var that you typed into the vbs..
Simple...

for multiple var just add:
the vbs: wshshell.run "test.bat " & a & b & var3 ect .. remember to set them a value
the batch: echo %1 %2 %3 ect..

BATCH TO VBS

the batch:

@echo off

set var=hello
wscript test.vbs %var%

this will sent 'hello' to test.vbs

the vbs:

dim ArgObj, a
Set fso = CreateObject("Scripting.FileSystemObject")
Set ArgObj = WScript.Arguments
a = ArgObj(0)

msgbox(a)

this will display 'hello' in the message box.

for multiple vars for batch to vbs easily add another var e.g
the batch: wscript test.vbs %var% %var2%
the vbs:

dim ArgObj, a, b
Set fso = CreateObject("Scripting.FileSystemObject")
Set ArgObj = WScript.Arguments
a = ArgObj(0)
b = ArgObj(1)
msgbox(a)
msgbox(b)

REMEMBER 0 counts

If you have any problems with these please pm or comment.

Comments

PaulusG1 (author)2015-08-13

SouthParkBucket (author)2015-07-05

About 6 years after the original post. Have just read this tutorial. Thumbs up to the original poster (aka IRule). The world needs more people like this - that show how to start making things happen and easy to follow (like in this tutorial). I didn't know any vbs scripting at all before. This tutorial is starting me off, and I think it is great.

VictorS10 (author)2015-06-08

I have made a program, it writes the basic HTML code for you in notepad all you have to do is edit.

Want to download the vbs file?

Visit: http://htmlbuddy.blogspot.com/

Mindrus (author)2014-10-07

Hey dude! I have made a password protect VBS. When you enter the correct password into the inputbox I want it to open a .bat file which is located in the same folder as the VBS. But I can never find the right way to open the .bat via vbs. Here is the current code:

Option Explicit

Dim returnvalue, pass, Shell

pass="test"

returnvalue=inputbox("Enter password")

If returnvalue=pass then

msgbox("Password correct!")

set Call=createobject("wscript.shell")

Call.run "Hack Client.bat"

set Call=Nothing

Else

do

msgbox("Password incorrect!")

loop

End If

Plz help me. Thanks!

VictorS10 (author)Mindrus2015-06-08

First where is the folder located?

To find this out right click go to properties and click general.

Reply to me then I can help fix this error.

bayuforest (author)2015-03-11

I have this script:

Set WshShell = CreateObject("WScript.Shell")
dim a
a = "C:\Program Files (x86)\MyProgram\MyApplication.jar"
WshShell.Run "javaw -jar " & a

but when i execute, i got error message:

"Error: Unable to access jarfile: C:\Program"

Please help. How to fix this?

Thanks

VictorS10 (author)bayuforest2015-06-08

Two errors found while I looked through your code.

First Error "a="

Second Error "C:\Program Files (x86)\MyProgram\MyApplication.jar"

How to fix:

First of all instead of "a=" type wshshell.run

Second type """C:\Program Files (x86)\MyProgram\MyApplication.jar""" where

"C:\Program Files (x86)\MyProgram\MyApplication.jar" is.

I did not understand the

WshShell.Run "javaw -jar " & a

So I hope this helps.

Dummiekid. (author)bayuforest2015-03-18

@bayuforest

Try to do it with escape characters, because you have spaces in the file path. So the code would be:

a = """C:\Program Files (x86)\MyProgram\MyApplication.jar"""

(yes with three " before and after the path)

Cheers and have fun coding.

NicholasV1 (author)2014-12-31

Can you tell me how to make the red X work please? I am just trying to close my message box

vvaradi (author)2014-12-21

visual basic 10.

yaminip (author)2014-08-24

I was searching for this a very long time.. Thanks U saved my time

stephen.waldron.144 (author)2014-08-02

Does the case look for the actual variable, and not the value?

Cobalt59 (author)2011-02-27

How would I end this code?

a=InputBox("Do you like Cheesecake?:","Var")
if var=yes then
msgbox ("YAY! I like cheesecake too!")
if var=no then
msgbox ("Aww..... You suck. Cheesecake is the best!")

It's probably something obvious.

iRule (author)Cobalt592011-04-02

i looked over this script and it was kind of right but the if shouldn't be var= it should be a as that is the input box name. Also it took me a while to work out why it wasn't working but turns out that you hadn't declared the var and that was last thing i thought of lol, here it is working.
..........
dim a
a=InputBox("Do you like Cheesecake?:","title")
if a=("yes") Then
 msgbox("YAY! I like cheesecake too!")
elseif a=("no") Then
msgbox("Aww..... You suck. Cheesecake is the best!")
else
msgbox("Enter yes or no!")
End If 
..........
hope it helped, sorry for the slow reply.

mvestergaard (author)iRule2014-03-14

I know it's two years ago but I've played a bit a made this


dim a

a=InputBox("Do you like Cheesecake?:","title")

if a=("yes") Then

msgbox("YAY! I like cheesecake too!")

elseif a=("Yes") Then

msgbox("YAY! I like cheesecake too!")

elseif a=("No") Then

msgbox("Aww..... You suck. Cheesecake is the best!")

elseif a=("no") Then

msgbox("Aww..... You suck. Cheesecake is the best!")

else

msgbox("Enter yes or no!")

CreateObject("WScript.Shell").Run("""cheesecake.vbs""")

End If

Hope it's good :) It'll loop (if filename is cheesecake.vbs) until you write "Yes", "yes", "No" or "no" :)

Cobalt59 (author)iRule2011-04-21

SORRY for the REALLY slow reply. Thanks man!

aantunovic (author)Cobalt592011-07-25

End If.

Jason the marksman (author)2014-02-03

A little bit complex.

iRule (author)2009-10-30

not that i approve of freezing a pc lol but can't you just do:

do
set x = CreateObject("WScript.Shell")
x.run "lol.bat"
loop

that's if you have a bat file named lol

hex3235 (author)iRule2011-11-03

or make a batch file containing

@echo off
:1
1.bat
goto :1

assuming the batch file is called 1.bat
im 13 and i know this i thoght someone would have thoght of that LOL

wrs715 (author)iRule2010-07-31

LOL Scripts and programs like this have existed for a long time. It's known as a "rabbit hunt", where scripts spawn children (which in turn spawn children) and so on, until system resources are eaten up. The better admins (Hackers?) make a game of killing parents before the system locks up.

si13ntsl4y3r (author)2010-12-01

I could barely understand what it was you were saying about the vbs to batch conversion. I am trying to find a good converter online but f2ko's vbs to exe doesn't work(I have a batch to exe so I could convert vbs to batch to exe). I looked at this method and am trying to figure it out but you didn't say what I should do. I copied and pasted the vbs code into notepad and created the vbs, and it works when I run it, but what do I type in on the line? A msgbox script? One word? I also tryed making a file called test.bat and put in in the same file folder, opened the vbs tryed a msgbox script and word and nothing happened. I then tryed pasting the "Batch" code (you didn't say what that is for either) in test.bat and all it does is pop up some cmd window that says whatever I typed into the vbs (and not a vbs window) somethin it also says sometimes is an error with line 5 character 1. What is the correct Character? Please give me a full explaination on exactly what I sould do. Thanks

iRule (author)si13ntsl4y3r2011-04-02

i am confused as to why you are confused ? Are you trying to make a vbs into a exe ? if so, then if the f2ko converter does not work i do not know of what else to use, as i do very little scripting in actual vbs, meaning i have little need for them as exes.

lanky.ninja (author)2010-08-24

set shellobj = CreateObject("WScript.Shell") shellobj.run "notepad" shellobj.sendkeys "I " wscript.sleep 1000 shellobj.sendkeys "S" wscript.sleep 1000 shellobj.sendkeys "E" wscript.sleep 1000 shellobj.sendkeys "E " wscript.sleep 1000 shellobj.sendkeys "YOU!!! " wscript.sleep 1000 shellobj.sendkeys "{ENTER}" wscript.sleep 4000 shellobj.sendkeys "{ALT+F4}"

erikals (author)2010-02-16

A great tutorial
btw, can you send me the code of the batch notepad i modified for you? I recently upgraded to a better pc and lost the file
thx
 

hg341 (author)2009-07-15

Set wshShell =wscript.CreateObject("WScript.Shell")
do
wscript.sleep 200
wshshell.sendkeys "{NUMLOCK}"
wscript.sleep 100
wshshell.sendkeys "{CAPSLOCK}"
wscript.sleep 100
wshshell.sendkeys "{sCROLLLOCK}"
wscript.sleep 200
wshshell.sendkeys "{sCROLLLOCK}"
wscript.sleep 100
wshshell.sendkeys "{CAPSLOCK}"
wscript.sleep 100
wshshell.sendkeys "{NUMLOCK}"
loop

turn off numlock and run that

Radobot (author)hg3412009-11-21

Do you mean SendKeys method?

iRule (author)hg3412009-07-16

That's a lot like my loader:
LED Loader

so what do you want it to do?

hg341 (author)iRule2009-07-17

.... i never saw that i did 'cuz i was funny as hell

Radobot (author)2009-10-27

Do you know the script that find a path where is a script saved?
(Because in vb6 it is in var AppPath) 

iRule (author)Radobot2009-10-28

i don't quite get you ....
if you want a path can't you just type it in without the app.path ?
then again I've never used VB6 so I'm still not sure what app.path does.

muttyfutty (author)2009-10-02

is it possable to replace a shortcut target in VB? Whats the code? thanks

iRule (author)muttyfutty2009-10-02

you want to change where a shortcut goes to ?

Set sh = CreateObject("WScript.Shell")
Set shortcut = sh.CreateShortcut("shortcut.lnk")
shortcut.TargetPath = "C:\where eva"
shortcut.Save

muttyfutty (author)iRule2009-10-02

yes, like poping up a mesagebox when I accidently click Internet explorer (I use Firfefok)lol is that the code? what other "objects" can I create? thanks a bunch sorry for all the quetions! Mike

iRule (author)muttyfutty2009-10-04

yes that's the code , just save that script to a .vbs then change shortcut.ink to the name of your file and change C:\where eva to the new location you want the shortcut to lead ...
e.g: shortcut.TargetPath = "C:\folder\test.vbs"

the get object is used for certain functions:
example
different scripts need to call different functions.
e.g the CreateObject("SAPI.SpVoice") is used to make the pc talk.

muttyfutty (author)iRule2009-10-04

Oh, Thanks alot for taking the time to reply! Thanks again :)

animan1 (author)2009-08-12

hey do you know how to set a button like if i did yes and no buttuns then if i press yes some thing will come up but if i press no somethingelse will com up

iRule (author)animan12009-08-15

simple version:


Set objShell = CreateObject("Wscript.Shell")
msg = Msgbox("Press yes to say hello.",36, "Hello?")
If msg = vbYes Then
msgbox("Hello =]")
Else
Wscript.Quit
End If

Says hello if you press yes
Quits if you press no
Does nothing if you press the X .. so you have to answer =]
Also: LINK - will help

animan1 (author)iRule2009-08-17

is there anything simpler?

iRule (author)animan12009-08-20

no that the simplest way that i know of sorry =/
btw its : dont need the top line of old 1


msg = Msgbox("Press yes to say hello.",36, "Hello?")
If msg = vbYes Then
msgbox("Hello =]")
Else
Wscript.Quit
End If

animan1 (author)iRule2009-08-20

thank you irule

Psychic Master (author)2009-07-14

this is a great tutorial, and it could finally get me going in vbs, thanks a ton!

iRule (author)Psychic Master2009-07-16

Glad i could help

About This Instructable

172,868views

23favorites

License:

More by iRule:
Add instructable to: