VB Script
VB Script
<html>
<head>
<title>My First VBScript Code!</title>
</head>
<body>
<script type="text/vbscript">
document.write(Hello my name is amrit")
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body style="background-color:lightgrey;">
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<a href="http://www.w3schools.com">Contact us </a>
<img src="w3schools.jpg" alt="W3Schools.com" width="104"
height="142">
</body>
</html>
Form
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<inputtype="radio"name="gender"value="male"checked>Male<
br>
<inputtype="radio"name="gender"value="female">Female<br>
<button type="button" >Click Me!
</form>
String Concatenation
example
Dim x
X =inputbox (what is your name)
Msgbox the name you entered is &
x & .
HTML
html>
<head>
<title>
</title>
</head>
<body>
<
</body>
</html>
.
</script>
<html>
<head>
<title>My First VBScript Code!</title>
<meta http-equiv="x-ua-compatible" content="IE=10">
</head>
<body>
<script type="text/vbscript">
document.write(Hello my name is amrit")
</script>
</body>
</html>
Option explicit
You can assign value to the variable
without declaring it, its not good
programing practise
Option Explicit statement at the
beginning of your code so that you will
be forced to declare all your variables
<html>
<body>
<script type="text/vbscript">
Option Explicit
Dim markE, markM, markT
markE=90
markM=86
markT=markE+markM
document.write("Your marks in English is " & markE & "." &
"")
document.write("Your marks in Mathematics is " & markM & "."
& "")
document.write("Your total marks is " & markT & ".")
</script>
</body>
</html>
Vbscript syntax
No semicolon
Not case sensitive, Javscript is case
senstive
Double slashes for comments in Java //
or /* .. */
+ and &
Dim x,y,result
X = 12 both are string so it concatenate
Y = 4
Result =x+y
Msgbox result
124
----------------------------------------------------------------------X =12
Y=4
both are integers so addition
Result = x+y
Msgbox result
Dim x,y,result
X = 12
one is string and one is integer
Y = 4
Result =x+y
Msgbox result
16
---------------------------------------------------Dim x,y,result
X = 12
one is string and one is integer
Y = hello
Result =x+y
Msgbox result
type mismatch error
IF ..THEN
dim x
X =3
If x > 10 then
Msgbox (hello class)
End if
IF then else
If time > 12 then
MSGBOX (good afternoon)
Else
msgbox (good morning)
End if
Option explicit
Dim age
Age = inputbox (enter your age)
If age < 16 then
Msgbox you are baby
Else
Msgbox ya you can drive
End if
If.....Then.......ElseIf
If.....Then.......ElseIf
if you have to select one of many
blocks of code to execute.
example
option explicit
dim age
age = inputbox ("enter your age")
if age <16 then
msgbox "stay home"
elseif age > 16 and age <60 then
msgbox "ya u can drive"
elseif age > 60 and age <80 then
is Sunday")
is Monday")
is Tuesday")
is Wednesday")
is Thursday")
is Friday")
is Saturday")
<script type="text/vbscript">
Dim myName
myName = "Charles"
Select Case myName
Case "Bob"
document.write("Been busy Bob?")
Case "Sara"
document.write("Seen any slick sunglasses Sara?")
Case "Charles"
document.write("Did you chuck your chowder Charles?")
End Select
</script>
Dim a
A = inputbox (what is amrit fav fruit)
Select case a
Case Mango
Msgbox Correct good job
Case Else
Msgincorrect
End select
For-Next Loop
For i = 1 To 5 Step 1
document.write("The number is " & i &
"")
Next
Do until
Dim x
x=1
Do
Msgbox x
X = x+1
Loop until x = 5
instr
Dim x
x = you rock the world
Instr (x, rock)
Output 5
right
Syntax
Right(string,length)
Example
Dim rose
Rose = one is all love
Msgbox RIGHT (rose, 3)
Output ove
MORE
ucase
dim z
Z = have a good day
Msgbox ucase
Output : HAVE A GOOD DAY
Len
dim z
Z = have a good day
Msgbox len (z)
Output 15
Dim x
X = 12
Msgbox typename integer also try x
=12.56,
Dim y
Y = sjss
Msgbox typename string also try x =true
Dim x
X = inputbox (enter the value)
Msgbox typename (x)
//string
Y =cint (x)
Msgbox typename (y)
//integer
Class: Scripting.
FileSystemObject
It is used to gain access to the computer file
system
To deal with Microsoft file system, it is required to
use this class
Create folder in file system
dim fobj1
set fobj1 =createobject
("Scripting.filesystemobject")
fobj1.createfolder (path)
set fobj1 = nothing
Read char
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.read (1)
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing
readLine
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.readline
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing
readAll
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.readline
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing
objexcel.workbooks.Save
Objexcel.activeworkbook.close
Objexcel.quit
set objexcel = nothing