Instructables

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.
 
Remove these adsRemove these ads by Signing Up

Step 1: 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..
Cobalt593 years ago
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)  Cobalt593 years ago
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.

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 iRule3 years ago
SORRY for the REALLY slow reply. Thanks man!
End If.

A little bit complex.

Radobot4 years ago
Do you know the script that find a path where is a script saved?
(Because in vb6 it is in var AppPath) 
iRule (author)  Radobot4 years ago
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.
Radobot iRule4 years ago
I need a variable in that is a way where is the script saved,
because that script is 'frozing' a computer and works like this:

Set x = CreateObject(Wscript.Shell)
x.Run way where is script saved
Do
Loop 'looping forever - forzing
iRule (author)  Radobot4 years ago
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 iRule2 years ago
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 iRule4 years ago
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.
Radobot iRule4 years ago
Great idea! I will make this:

Batch file:
s:
start "Forze.vbs"
goto s

in same folder vbscript:
Do
Loop

OR

For i = 1 To 3
i = 2
Next i
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)  si13ntsl4y3r3 years ago
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.ninja3 years ago
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}"
erikals4 years ago

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
 

hg3415 years ago
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 hg3414 years ago
Do you mean SendKeys method?
iRule (author)  hg3415 years ago
That's a lot like my loader:
LED Loader

so what do you want it to do?
hg341 iRule5 years ago
.... i never saw that i did 'cuz i was funny as hell
muttyfutty4 years ago
is it possable to replace a shortcut target in VB? Whats the code? thanks
iRule (author)  muttyfutty4 years ago
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
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)  muttyfutty4 years ago
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.
Oh, Thanks alot for taking the time to reply! Thanks again :)
animan14 years ago
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)  animan14 years ago
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 iRule4 years ago
is there anything simpler?
iRule (author)  animan14 years ago
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 iRule4 years ago
thank you irule
this is a great tutorial, and it could finally get me going in vbs, thanks a ton!
iRule (author)  Psychic Master5 years ago
Glad i could help