VB is not case sensitive.
Set objWMIService = GetObject("winmgmts:")
is the same as
set objwmiservice = GETOBJECT("winmgmts:")
However, strings are of course case sensitive as usual.
' report free disk space Const CONVERSION_FACTOR = 1048576 Set objWMIService = GetObject("winmgmts:") Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'") FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR Wscript.Echo Int(FreeMegaBytes)
In VB, a line ending char means end of a statement. To break a long line but not terminate the statement, use “_” as the last char in a line.
To join strings, use “&”.
' example of getting folder content and file name Set FSO = CreateObject("Scripting.FileSystemObject") Set Folder = FSO.GetFolder("C:\Windows") Set FileList = Folder.Files For Each File in FileList Wscript.Echo File.Name & ", " & File.Size & ", " & File.DateLastModified Next
' string manipulations xx = "test 123" Wscript.Echo len(xx) ' num of chars Wscript.Echo left(xx, 3) ' get parts of string of 3 chars, starting from left Wscript.Echo right(xx, 3) ' starting from right Wscript.Echo mid(xx, 3, 2) ' get parts of string, starting from right Space(9) ' generates 9 space chars Trim(" some ") ' remove leading and ending spaces LTrim(" abc ") ' remove spaces from left RTrim(" def ") ' remove spaces from left InStr(" something in water", "in") ' find the position of a substr ' lcase() ucase()
to format numbers, use e.g. “FormatNumber(3.142,0)”.
' example of using loop Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:/Users/xah/web/visual_basic/basics.html", ForReading) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline Wscript.Echo strNextLine Loop
' example of defining a function Function myFunc(ByVal xx) myFunc = (xx * (9/5)) + 32 End Function xx = 20 yy = myFunc(xx) wscript.Echo yy
Variable needs not be declared. But for writing large scripts, you can force them to needs to be declared. To force, use “Option Explicit”, then for each var, use “Dim”.
' example of forcing declaration for variables Option Explicit Dim xx Dim yy xx = 11 yy = ff(xx) Wscript.Echo yy Function ff(ByVal xx) ff = (xx * (9/5)) + 32 End Function
To declare a var never changes, use “Const”.
Const MY_LOVE = "Jenny" wscript.echo MY_LOVE
VBS has some built-in constants. e.g.
ConfirmDelete = MsgBox ("Are you sure??", _ VbYesNo OR VBDefaultButton2, "Delete all files") If ConfirmDelete = VbNo then Wscript.Quit End If
the following semes to be some common constant: vbcrlf, vbtab.
' example of extracting date parts Wscript.Echo Now Wscript.Echo "Year: " & DatePart("yyyy" , Now) Wscript.Echo "Quarter: " & DatePart("q", Now) Wscript.Echo "Month: " & DatePart("m" , Now) Wscript.Echo "Day of Year: " & DatePart("y" , Now) Wscript.Echo "Day: " & DatePart("d" , Now) Wscript.Echo "Weekday: " & DatePart("w" , Now) Wscript.Echo "Week of Year: " & DatePart("ww" , Now) Wscript.Echo "Hour: " & DatePart("h", Now) Wscript.Echo "Minute: " & DatePart("n" , Now) Wscript.Echo "Seconds: " & DatePart("s" , Now)blog comments powered by Disqus