VBscripting

Check out more papers on Computer Engineering Computer Computer Programming

 

Program description: Script to show the week of the day, the present day of the week and also the array that outputs all weeks of the week. It also explores the input and output methods using VBscripting and the decision making using the IF-ELSE.

Note The programme has to be copied to notepad or notepad and saved to a file format. The file format should be vbs that can be achieved by saving as from the file menu. With notepad it is good practice to use quotes (“FileName.vbs”) while naming the file for the extension to work correctly

Introduction

'The section covers variables, constants and data type

Option Explicit

Dim Message, result

Dim Title, var1, var2

Define dialog box variables.

Message = "Please enter a path"

Title = "WSH sample user input "

Var1 = "User input canceled"

var2 = "You entered:" & vbCrLf

Ready to use the InputBox function

InputBox(prompt, title, default, xpos, ypos)

prompt:    The text shown in the dialog box

title:     The title of the dialog box

default:   Default value shown in the text box

xpos/ypos: Upper left position of the dialog box

If a parameter is omitted, VBScript uses a default value.

result = InputBox(Message, Title, "C:\Windows", 100, 100)

Evaluate the user input.

If result = "" Then    Canceled by the user

WScript.Echo var1

Else

WScript.Echo var2 & result

End If End

Loop structures and arrays in details using the days of the week.'The section of the programme checks on the day of the day and remind the user by using looping and array

Also constants and variables are explored further in the section

Define constant and variables.

Const title = "Dear, John !"    User's name can be changed according to client needs

Dim text

the variable cNotes is used as an array that contains messages for each day of the week.

Dim cNotes

Now we define an array with the daily messages.

cNotes = Array 

"Hey, it's Sunday. Please take a rest, my friend.", 

"It's Monday. Let's begin the week.", 

"Oops, it's Tuesday. One day of the week is gone.", 

"Don't worry, it's Wednesday.", 

"Hurray, it's Thursday.", 

"Thank goodness it's Friday.", 

"Saturday! Why don't you relax this weekend?")

Here we define the date within the welcome message.

text = WeekDayName(Weekday(Now()), False, 1) & 

", " & MonthName(Month(Now)) & " " & 

Day(Now()) & ", " & 

Year(Now())

Now we append a custom message to the date.

text = text & vbCrLf & vbCrLf & cNotes(Weekday(Now()) - 1)

Display the message.

MsgBox Text, vbOKOnly + vbInformation, title

 End

The section is about vbscript input and output methods are

File Input-Output method implemented, decision making statements are explored using the if-else function and the procedures are explored.

VBScript Write File

Option Explicit

Dim objFSO, objFolder, objShell, objTextFile, objFile

Dim strDirectory, strFile, strText

strDirectory = "f:\logs3"

strFile = "\Summer.txt" the file name to be created on to your computer

strText = "You are smart on your work." 'The test that need to be written on the file generated above.

Create the File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject")

Check that the strDirectory folder exists

If objFSO.FolderExists(strDirectory) Then

Set objFolder = objFSO.GetFolder(strDirectory)

Else

Set objFolder = objFSO.CreateFolder(strDirectory)

WScript.Echo "Just created " & strDirectory

End If

If objFSO.FileExists(strDirectory & strFile) Then

Set objFolder = objFSO.GetFolder(strDirectory)

Else

Set objFile = objFSO.CreateTextFile(strDirectory & strFile)

Wscript.Echo "Just created " & strDirectory & strFile

End If

set objFile = nothing

set objFolder = nothing

OpenTextFile Method needs a Const value

ForAppending = 8 ForReading = 1, ForWriting = 2

Const ForAppending = 8

Set objTextFile = objFSO.OpenTextFile _

(strDirectory & strFile, ForAppending, True)

Writes strText every time you run this VBScript

objTextFile.WriteLine(strText)

objTextFile.Close

Bonus or cosmetic section to launch explorer to check file

If err.number = vbEmpty then

Set objShell = CreateObject("WScript.Shell")

objShell.run ("Explorer" &" " & strDirectory & "\" )

Else WScript.echo "VBScript Error: " & err.number

End If

WScript.Quit

End of VBScript to for vbscript for file input and output method, decision making and methods.

Function section that displays a message box

Answer = getResponse()

Function getResponse()

Dim answ

timeOut = 10

title = “error!”

button = 2

create object.

Set w = WScript.CreateObject(“WScript.shell”)

getResponse = w.Popup( “Write failure. Try again?”.timeOut.title.button)

End Function end of the Function method

Did you like this example?

Cite this page

VBscripting. (2019, Oct 10). Retrieved December 15, 2024 , from
https://studydriver.com/vbscripting/

Save time with Studydriver!

Get in touch with our top writers for a non-plagiarized essays written to satisfy your needs

Get custom essay

Stuck on ideas? Struggling with a concept?

A professional writer will make a clear, mistake-free paper for you!

Get help with your assignment
Leave your email and we will send a sample to you.
Stop wasting your time searching for samples!
You can find a skilled professional who can write any paper for you.
Get unique paper

Hi!
I'm Amy :)

I can help you save hours on your homework. Let's start by finding a writer.

Find Writer