Fun With Easy VBS!!!

153,937

34

57

Posted in TechnologySoftware

Introduction: Fun With Easy VBS!!!

A Beginners guide to fun, and easy to do visual basic script. VSB may seem hard to some, but in fact it is really easy. Windows can even run it without a compiler! If you know javascript, it should be really easy for you.

Step 1: Basic Message Boxes

Ok so lets go over the Basics.

Have you ever seen a message box like the one below,
you probaly have. These are used to alert users of whatever you want to say.

You can create your own by using the Function Msgbox

So the code to a message box that says hi would look like this,

Msgbox("hi")

Alternately you can make an unkillable message box by making a simple loop by using the Do, Loop commands.

Now that we covered message boxes, lets move to more advanced message boxes.

Step 2: Games and Stuff

If you ever wonder how to make text games than look no further. I might be wrong, but I believe that they used vbs. Now a lot of vb scripts use a compiler, but windows doesn't need one. Just type cscript before the file path, now if you know how to make bat files, then you can automate this process, but we wont get into that in this instructable.

Here is a little game i made

Option Explicit

Dim Answer

'types the question

Wscript.StdOut.Write "Nice Day Isin't it? "
answer = Wscript.StdIn.ReadLine

'if you type yes t

If answer="yes" Then
Wscript.echo ("I hope it stays this way")

'If you type no

ElseIf answer="no" Then

Wscript.echo ("I hope it gets better")

'anything else

Else

Wscript.Echo ("GoodDay to you")

'end

End If

now basically save this as .vbs and use the method before to run it.

Step 3: The Auto Typer

This code it to type something over and over again. I don't know how it is useful, but if you run it, you'll have to kill it in the task list

set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"

do

shellobj.sendkeys "Y"
wscript.sleep 200
Shellobj.sendkeys "o "
wscript.sleep 200

loop

Step 4: The Matirx Has You

Ever wondered how to make you pc talk to you?

well copy this



option explicit
On Error Resume Next

' declare variables
dim wshshell, title, mystring, length, position, tmp, loopA, loopB, linesToWrite, columns, objUser, objSysInfo, myName, myNameLength, col1, col2, col3, col4, col5, maximum, elements

set wshshell = wscript.createobject("wscript.shell")
title = "the matrix"

wshshell.SendKeys "{ALT}"+"{ENTER}"

' get user info, used to get the name
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

' ---------------------------
sub waitfor(var)
' set up window, set title, wait for it to complete
tmp = false
do until tmp
wscript.sleep 1000
tmp = wshshell.appactivate(var)
loop
end sub

' ---------------------------
sub matrix(elements)
' print random numbers to make "the matrix" look
' assumes elements is divisible by 5
columns = 5
col1 = true
col2 = true
col3 = true
col4 = true
col5 = true
randomize()
linesToWrite = elements / columns
for loopA = 1 to linesToWrite
for loopB = 1 to columns
Select Case loopB
Case 1
If col1 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 2
If col2 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 3
If col3 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 4
If col4 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 5
If col5 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
End Select
wscript.sleep 5
next
wscript.Echo

' This flip-flops columns to display 20 percent of the time, independently of each other.
maximum = 0.20
If rnd() < maximum Then
If col1 = true Then col1 = false Else col1 = true
End If
If rnd() < maximum Then
If col2 = true Then col2 = false Else col2 = true
End If
If rnd() < maximum Then
If col3 = true Then col3 = false Else col3 = true
End If
If rnd() < maximum Then
If col4 = true Then col4 = false Else col4 = true
End If
If rnd() < maximum Then
If col5 = true Then col5 = false Else col5 = true
End If
next
wscript.Echo
wscript.Echo
end sub

' ---------------------------
sub key(msg)
' do printouts of "the matrix" messages, and erase them
wscript.sleep 1500
length = len(msg)
for position = 1 to length
wshshell.SendKeys mid(msg, position, 1)
wscript.sleep 250
next
wscript.sleep 3000
for position = 1 to length
wshshell.SendKeys "{BACKSPACE}"
wscript.sleep 75
next
end sub

' ---------------------------
' start "main" execution here

waitfor(title)

For loopA = 1 To 4
Select Case loopA
Case 1
mystring = "wake up, Neo..."
Case 2
mystring = "The matrix has you..."
Case 3
mystring = "Follow the white rabbit..."
Case 4
mystring = "Knock knock..."
End Select
' now print out the correct string
key(mystring)
next

'_ _ _ _ _ _ _ _ _ _

wscript.quit()


save it to C:\temp as matrix.vbs

the create the bat files


@echo off
color 0a
title the matrix
set /p matrix= | cscript /nologo

(Replace this with your path to the vbs file)

exit
set /p matrix=


Name that RabbitHole.bat. Then run it!!

this is an cool, but advanced thing to do with vbs

I just put it in because it is awesome

Step 5: End

There you go


The skys the limit to what you can do

2 People Made This Project!

Recommendations

  • Spotless Contest

    Spotless Contest
  • Trash to Treasure

    Trash to Treasure
  • Science of Cooking

    Science of Cooking
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

57 Comments

is keeps saying script file not specified

i want to create a riddle box but its seems to skip a part this is the code i used

option explicit
dim main, age

do
main=inputbox("What goes up and never comes down?","Riddle!!","Answer goes here!")

if main = "" then
msgbox"Oh! boy, you are wrong."
elseif main = "age" or main = "Age" then
msgbox"You are right!!!",vbOkonly+vbSystemModal,"Riddle_Answer"
loop until main = "age" or "Age"
elseif main <> "age" then
msgbox"You are so wrong!",vbOKOnly+vbSystemModal,"Wrong_Answer."
else
msgbox"Oh! boy, you are wrong."

end if

1 reply

Its a bit late but here is the code for the riddle box:

Code.PNG

Yeah, I just have one problem. How do I get the Message box to appear in a random place?

Doesn't work.

Capture.PNGCapture1.PNG

i need help how can i serch an whole pc for an file and if found delete it and replace it with another

HI ALL can someone help me write a script that will ask questions like your name surname and than email option

HI ALL can someone help me write a script that will ask questions like your name surname and than email option

---------------------------------------------
Option Explicit
Dim OBJ,RID : Set OBJ=(CreateObject("Wscript.Shell"))
Do : RID=(InputBox("What goes up and never comes down?","Riddle!!","Answer goes here!"))

If(LCase(RID))<>("age") Then
If(RID)=("") Then WScript.Quit
If(RID)=("Answer goes here!") Then WScript.Quit
OBJ.Popup("Oh! boy, you are wrong."),2,"Wrong, try again.",0
Else : OBJ.Popup("You are right."),0,"Riddle Answer.",0
Exit Do
EndIf
Loop : WScript.Quit
---------------------------------------------
note: obj.popup will work compiled, msgbox will not.

i want to create a riddle box but its seems to skip a part this is the code i used

option explicit
dim main, age

do
main=inputbox("What goes up and never comes down?","Riddle!!","Answer goes here!")

if main = "" then
msgbox"Oh! boy, you are wrong."
elseif main = "age" or main = "Age" then
msgbox"You are right!!!",vbOkonly+vbSystemModal,"Riddle_Answer"
loop until main = "age" or "Age"
elseif main <> "age" then
msgbox"You are so wrong!",vbOKOnly+vbSystemModal,"Wrong_Answer."
else
msgbox"Oh! boy, you are wrong."

end if

Can anyone tell me (a n00b) how to make the Red X work on my message box?

1 reply

well if your using the MsgBox you would want to put a ,0+16, for example A=MsgBox("hello sir",0+16,"computer")

hello,

Im trying to make a msgbox script where when you open it it says something like usual, then we you close it it waits a few seconds and opens again. Is this possible? if so can I have some help?

1 reply
user

do you want the loop to be infinite??? if so, use this:
'---------------------------------------
Set objShell = WScript.CreateObject( "WScript.Shell" )
msg = "what you want the box to say"
wait = 1000 'this is how long it waits before the next text box, 1000 = 1 second

Do
MsgBox(msg)
wscript.sleep wait
Loop
'--------------------------------------

if you don't want it to be infinite:
'--------------------------------------
Set objShell = WScript.CreateObject( "WScript.Shell" )
msg = "what you want the box to say"
wait = 1000 'this is how long it waits before the next text box, 1000 = 1 second
loops = 10 'how many times you want the loop to go


count = 0
Do while count < loops
MsgBox(msg)
wscript.sleep wait
count = count + 1
Loop
'--------------------------------------

Hi

I just want to know how to make a message box do something depending on what option you select (yes,no,cancel)

Say for example I make a message box and a person gives an input, depending on which input it will open another message box eg. "Are you happy" when the person selects the "Yes" option it goes to another message box saying "That's good" and when they say no, something else comes up.

Any help on this would be much appreciated
Thanks in advance

2 replies

0 - ok button only 1 - ok and cancel 2 - abort, retry and ignore 3 - yes no and cancel 4 - yes and no 5 - retry and cancel
                                                      msgbox"Hi",3,"mthom41"
these set button values in a msgbox between quotes. the 3 sets it to yes, no, and cancel.
dim var1  
var1=msgbox ("am i cool?,3,"mthom41")
this creates a variable called var1, then sets its value to a msgbox output.
1 = OK 2 = Cancel 3 = Abort 4 = Retry 5 = Ignore 6 = Yes 7 = No
these are button return values.
now... you need to combine these with an if.. then.. statement and polish it up a little

dim decision
decision=msgbox ("'Sigghhhh...' I should try going somewhere." &vbnewline& "I'll take a step... or i could not, and sit down",3,"Yes=step No=sit and think. Finally, Dialogue!")
if decision=6 then
msgbox "'AAAAHH!!!!' I tripped over a cable!!!",48,"Falling....."
wscript.sleep 4000
msgbox ".............",48, "you are knocked out :("
else if decision=7 then
msgbox"I'll just sit here and wait...zzzzzzz",64,"Hope i don't fall asleep..zzzzzzz"
wscript.sleep 4000
else if decision=2 then
msgbox"You can't avoid it"
end if
end if
end if

this makes a series of boxes... well just look at the pics...



i1.JPGicancel.JPGino.JPGiyes.JPG

sorry. pic 1 is the main box, 2 is cancel, 3 is no, and 4 is yes

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

you arnt actually teaching anyone how to create a script. what is the point in this instructable? im wanting to know what to put at the end of a fake virus script i have made in order to shutdown the computer after 60 secs. please help me

1 reply

to get a shutdown timed to 60 seconds you mean? are you using batch or vbs?