Friday, March 25, 2011

Learn - VB Script

How to Run VB Script:

Write Script in Notepad ---> Save it as filename.vbs -----> Double click on the file-filename.vbs


'Assignment Date:25/03/2011
'Prgm to demonstrate concatenation function
'----------------------------------------------------

option explicit
dim vfn, vmn, vln, vtmp

vfn=inputbox("Enter First Name :")
vmn=inputbox("Enter Middle Name :")
vln=inputbox("Enter Last Name :")

vtmp="Full Name is : "

msgbox vtmp&vln&" "&vmn&", "&vfn&"."

'Assignment Date:25/03/2011
'Prgm to demonstrate concatenation and Newline function
'----------------------------------------------------------------

option explicit
dim vfn, vln, vage, vdob, vtmp

vfn=inputbox("Enter First Name :")
vln=inputbox("Enter Last Name :")
vage=inputbox("Enter Age :")
vdob=inputbox("Enter DOB :")

vtmp="The Details are : "&vbnewline
vtmp=vtmp&vln&", "&vfn&vbnewline
vtmp=vtmp&vage&" yrs"&vbnewline
vtmp=vtmp&vdob

msgbox vtmp


'Assignment Date:25/03/2011
'Prgm to demonstrate Simple "IF" Statement
'---------------------------------------------------

option explicit
dim vnum

vnum=cint(inputbox("Enter a Number :"))

if (vnum mod 2)<>0 then msgbox "Odd Number"



'Assignment Date: 28/03/2011
'Demonstrate " if...elseif " (Smallest No. among three no.s)
'-------------------------------------------------------------------------------
Option explicit
dim vnum1, vnum2, vnum3

vnum1=cint(inputbox("Enter First Number: "))
vnum2=cint(inputbox("Enter Second Number: "))
vnum3=cint(inputbox("Enter Third Number: "))

if (vnum1=vnum2) and (vnum2=vnum3) then

   msgbox "All No.s are EQUAL"

elseif(vnum1<=vnum2) and (vnum1<=vnum3) then

  msgbox vnum1&" is Least"

elseif(vnum2<=vnum1) and (vnum2<=vnum3) then

      msgbox vnum2&" is least"

     else
    
      msgbox vnum3&" is least"
end if




'Assignment Date: 28/03/2011
'Demonstrate " if...elseif " (Highest No. among Four no.s)
'------------------------------------------------------------------------------------
Option explicit
dim vnum1, vnum2, vnum3, vnum4

vnum1=cint(inputbox("Enter First Number: "))
vnum2=cint(inputbox("Enter Second Number: "))
vnum3=cint(inputbox("Enter Third Number: "))
vnum3=cint(inputbox("Enter Fourth Number: "))

if(vnum1=vnum2) and (vnum2=vnum3) and (vnum3=vnum4) then

      msgbox "All No.s are EQUAL"

elseif(vnum1>=vnum2) and (vnum1>=vnum3) and (vnum1>=vnum4) then

      msgbox vnum1&" is Highest"

elseif(vnum2<=vnum1) and (vnum2<=vnum3) and (vnum2<=vnum4) then

      msgbox vnum2&" is Highest"

elseif(vnum3>=vnum4) and (vnum3>=vnum4) and (vnum3>=vnum4) then
    
      msgbox vnum3&" is Highest"

else

      msgbox vnum4&" is Highest"  

end if


'Assignment Date: 28/03/2011
'Demonstrate " Nested  if " 
'--------------------------------------------------------------------------------------
Option explicit
dim vnum1, vnum2, vnum3

vnum1=cint(inputbox("Enter First Number: "))
vnum2=cint(inputbox("Enter Second Number: "))

if(vnum1=vnum2) then

   msgbox " Numbers are Equal "

else

   if(vnum1<vnum2) then

      msgbox vnum1&" is less than "&vnum2

   else

      
     msgbox vnum1&" is greater"

     vnum3=cint(inputbox("Enter Third Number: "))

           if (vnum1>=vnum2) and (vnum1>=vnum3) then                            

               msgbox vnum1&"  is Highest"

           else
                 if(vnum2>=vnum1) and (vnum2>=vnum3) then

                     msgbox vnum2&"  is Highest"

                 else
    
                     msgbox vnum3&"  is Highest"
              
                 end if
        
           end if

    end if

end if


'Assignment date: 30/03/2011
'Demonstrate "Select case" statement (Calculator)
'------------------------------------------------------------------


Option explicit
dim vnum1, vnum2, vtmp, voptn, vsum, vsub, vmul, vdiv, vintdiv, vexp, vmod

vnum1=cint(inputbox("Enter 1st Number: "))
vnum2=cint(inputbox("Enter 2nd Number: "))

vtmp="Enter ur choice"&vbnewline&"1: Add"&vbnewline&" 2: Sub"&vbnewline&"3: Mul"&vbnewline&" 4: Div"&vbnewline

msgbox vtmp&" 5: IntDiv"&vbnewline&" 6: Exponent"&vbnewline&" 7: Mod"&vbnewline&" 8: Even/odd"

voptn=cint(inputbox("Enter ur Choice"))

select case voptn

case "1"
            vsum = vnum1 + vnum2
            msgbox "Sum of " &vnum1 &" And " &vnum2 &" = "&vsum

case "2"
            vsub = vnum1 - vnum2
            msgbox "Substraction of " &vnum1 &" And " &vnum2 &" = "&vsub


case "3"
            vmul = vnum1 * vnum2
            msgbox "Multiplication of " &vnum1 &" And " &vnum2 &" = "&vmul


case "4"
            vdiv = vnum1 / vnum2
            msgbox "Division of " &vnum1 &" And " &vnum2 &" = "&vdiv


case "5"
            vintdiv = vnum1 \ vnum2
            msgbox "Integer Division of " &vnum1 &" And " &vnum2 &" = "&vintdiv


case "6"
            vexp = vnum1 ^ vnum2
            msgbox "Exponent of " &vnum1 &" And " &vnum2 &" = "&vexp


case "7"
            vmod = vnum1 mod vnum2
            msgbox vnum1 &" Mod " &vnum2 &" = "&vmod


case "8"
            if(vnum1 mod 2)=0 then
            msgbox vnum1 &" is Even Number"
            else
            msgbox vnum1 &" is Odd Number"
            end if

            if(vnum2 mod 2)=0 then
            msgbox vnum2 &" is Even Number"
            else
            msgbox vnum2 &" is Odd Number"
            end if

case else
            msgbox " Invalid Entry ! "

end select

'Assignment date: 30/03/2011
'Demonstrate "For loop" statement => Print all odd no.s betwn 100 to 200
'--------------------------------------------------------------------------------


Option explicit
dim i,  vstro

for i=100 to 200 step 1

 
   if(i mod 2)<>0 then vstro=vstro&i&vbnewline
      
     
next

  msgbox "Odd no.s Between 100 and 200"&vbnewline &vstro



'Assignment date: 30/03/2011
'Demonstrate "For loop" statement => Print even no.s betwn 400 to 300
'------------------------------------------------------------------------------------------------ 

Option explicit
dim i, vstre, vcnt
vcnt=0

for i=400 to 300 step -1


   if(i mod 2)=0 then

      vstre=vstre&i&vbnewline

      vcnt=vcnt+1

      if(vcnt=20) then exit for

   else
    
      
   end if

next

  msgbox "Even no.s Between 400 and 300"&vbnewline &vstre



'Assignment date: 30/03/2011
'Demonstrate "For loop" statement => Print Factorial of a Number
'-------------------------------------------------------------------------------------------

Option explicit
dim fact, i, vnum

fact=1

vnum=cint(inputbox("Enter a Value"))

for i=1 to vnum step 1

  fact = fact * i

next

msgbox "Factorial of "&vnum&" is "&fact


'Assignment date: 30/03/2011
'Demonstrate "For loop" statement => Check given No. is Prime Number or Not
'--------------------------------------------------------------------------
------------------------------

Option explicit
dim vnum, vf, i

vnum=cint(inputbox("enter a number: "))

for i=2 to vnum-1 step 1
  
     if(vnum mod i)=0 then
     vf="No"
     exit for

     else
       vf="Yes"
     end if

next        
    
if (vf="No") then

   msgbox "The given number is Not a Prime"

else

    msgbox "The given number is a Prime"

end if


'Assignment date: 30/03/2011
'Demonstrate "For loop" statement => To generate Fibonacci Series
'--------------------------------------------------------------------------------------------------


Option explicit
dim vnum, i, j, k, vstr, x

i=0
j=1

vnum=cint(inputbox("enter a number: "))

vstr=i&", "&vbnewline&j&", "

for x=1 to vnum-2 step 1
  
    k=i+j
    vstr=vstr&vbnewline&k&", "
    i=j
    j=k

next        
  
    msgbox "Fibonacci Series: "&vbnewline&vstr


'Assignment date: 01/04/2011
'Demonstrate "Do while" loop => Print Factorial of a Number
'-----------------------------------------------------------------------------------------------


Option explicit
dim fact, i, vnum, vchoice

do

fact=1

vnum=cint(inputbox("Enter a Value"))

for i=2 to vnum step 1

  fact = fact * i

next

msgbox "Factorial of "&vnum&" is "&fact

vchoice=inputbox("Do you wanna Continue(y)?")

loop while vchoice="y"

'Assignment date: 01/04/2011
'Demonstrate "Do while" loop => Print Factorial of a Number
'--------------------------------------------------------------------------------------------------


Option explicit
dim fact, i, vnum, vchoice

vchoice=inputbox("Do you wanna Continue(y)?")

do while vchoice="y"

fact=1

vnum=cint(inputbox("Enter a Value"))

for i=2 to vnum step 1

  fact = fact * i

next

msgbox "Factorial of "&vnum&" is "&fact

vchoice=inputbox("Do you wanna Continue(y)?")

loop


'Assignment date: 01/04/2011
'Demonstrate "Do while loop"  => To Generate Natural No.s
'----------------------------------------------------------------------------------------------


Option explicit
dim vnum, i, vtmp, voptn

do

vnum=cint(inputbox("Enter a Number"))

for i=1 to vnum step 1

 vtmp=vtmp&vbnewline&i

next

 msgbox "Generated Numbers: "&vbnewline&vtmp

 voptn=inputbox("Do you wanna Continue(y)?")

loop while voptn="y"

'Assignment Date: 01/04/2011
'Demonstrate usage of " ARRAYS " in Vb Script (Accept 4 no.s & print in reverse order)
'------------------------------------------------------------------------------------------------------------------------


Option explicit
dim varr(3), i, tmp

for i=0 to 3 step 1

  varr(i)=cint(inputbox("Enter a value"))

next


for i=3 to 0 step -1

 tmp=tmp&varr(i)&vbnewline

next

 msgbox tmp

'Assignment date: 01/04/2011
'Demonstrate "ARRAYS"  => Sum of 5 numbers
'----------------------------------------------------------------------------------


Option explicit
dim Varr(4), i, vsum

for i=0 to 4 step 1

   varr(i)=cint(inputbox("Enter a number"))

next

for i=0 to 4 step 1

  vsum=vsum+varr(i)

next
 
  msgbox "Sum:  "&vsum

'Assignment Date: 08/04/2011
' Given string is Palindrome or Not
'------------------------------------------------------------

vstr1=inputbox("enter a string: ")

vstr2=strreverse(vstr1)

 if(vstr1=vstr2) then

    msgbox "Pal"

 else

    msgbox "Not Pal"

 end if

' Assignment Date: 08/04/2011
' Given  TWO strings are equal/ greater/ less
'----------------------------------------------------------------------

Option explicit
Dim vstr1, vstr2, vcmp

vstr1=inputbox("enter a string 1 : ")

vstr2=inputbox("enter a string 2 : ")

   vcmp=(strcomp(vstr1, vstr2))

select case vcmp

  case "0":
            msgbox "Both strings are Equal"

  case "1":
            msgbox "string1 is greater than string2"

  case "-1":
            msgbox "string1 is less than string2"

  case else:

            msgbox "***"

end select

' Assignment Date: 08/04/2011
' Find Highest among Five strings
'-------------------------------------------------------------
Option explicit
Dim vstr(4), i, vtmp, vhigh


for i=0 to 4 step 1

    vstr(i)=inputbox("enter a string: ")

next
 

for i=0 to 4 step 1

    vtmp=vtmp&vstr(i)&vbnewline

next
   
    msgbox vtmp


vhigh=vstr(0)

for i=0 to 4 step 1

     if(strcomp(vstr(i),vhigh)=1) then

       vhigh=vstr(i)   
  
    end if

next

  msgbox " Highest string is: "&vhigh


FSO - File System Object

'Assignment Date: 09/04/2011
'Working on FSO
'Create Folder
'---------------------------------------------------------------------

Option explicit
Dim vfso

set vfso=createobject("scripting.filesystemobject")

if vfso.folderExists("C:\test") then

    msgbox " Folder Exist     "

else

    msgbox " Folder Not exist "

    vfso.createFolder("C:\test")


end if

'Assignment Date: 09/04/2011
'Working on FSO
'Copy Folder only if the Source folder exist
'-----------------------------------------------------


Option explicit
Dim vfso, vsrc, vdes

vsrc=inputbox("Enter the Source folder path(absolute path) ")
vdes=inputbox("Enter the Destination folder path (absolute path) ")

set vfso=createobject("scripting.filesystemobject")


if vfso.folderExists(vsrc) then

    msgbox " Source Folder exist "
   
         vfso.copyFolder vsrc, vdes
   
    msgbox "Folder Copied !"

else

    msgbox " Source Folder is not exist ! "
   

end if

'Assignment Date: 09/04/2011
'Working on FSO
' Print Drive Information
'------------------------------------------------------------------------------------


Option explicit
Dim vfso, vdr, vdrive, drvname, drvtotsize, drvfreespc, Usdspc, vtemp

vdr=inputbox("Enter a Drive Name: ")


set vfso=createobject("scripting.filesystemobject")

set vdrive=vfso.getDrive(vdr)


drvname=vdrive.DriveLetter
drvtotsize=vdrive.totalSize/(1024*1024*1024)
drvfreespc=vdrive.FreeSpace/(1024*1024*1024)
Usdspc=drvtotsize-drvfreespc

vtemp="Drive Name:  "&drvname&vbnewline&"       "&vbnewline&"Total Size: "&drvtotsize&" GB"

msgbox vtemp&vbnewline&"Used Space: "&Usdspc&" GB"&vbnewline&"Free Space: "&drvfreespc&" GB" 

No comments:

Post a Comment