Yeshai Bouskila Just Another Site Of Mine

10Jan/110

To DIM or not to DIM that is the question, VBScript Variables

So I got an email from someone asking me based on the code snippets I place on my site why don't I "Option Explicit" and declare my variables....

So what are variables and what do you do with them?
I'm not going to write to much but pull from another location - Source
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).

So now that you know what variables are, let's talk about variable type's in VBScript's and why I don't use them...

In VBScript, all variables are of type variant, that can store different types of data, unlike other languages like VB that you need to declare the variable type like Integer, Decimal etc
You can declare variables in VBScript by starting you code with

Option Explicit
Dim MySpaceShip
MySpaceShip = "Some Odd Name or Value"

So in VB you would need to declare what your variable would hold something like

Dim MySpaceShip As String ' --> While string can be any value of characters and numbers....
Dim MySpaceShipSpeed As Integer ' --> My Space Ship's speed can only be in number values.....

There are more values but I won't get into that as I said VBScript variables are always variant's that can hold any value numeric or alphanumeric.

Some Rules
A variable can have a short name, something like Y would work but you should put some more details, let's say MySpaceShip.
Rules for VBScript variable names:
They have to begin with a letter!
The varicable cannot contain a period ".", the value they hold can
Length max is only 255 long

So back to VBScript and DIMing......
Why don't I? the answer is quite simple, I create all scripts with VBS Edit and I don't feel the need to declare anything (problems will start if you try to use the same variable twice without doing something with the old value)
So here is a good example of DIMing with and issue

'----------------------------------------------------------------------
' Yeshai Bouskila - Some Script
'----------------------------------------------------------------------
  	set objShell = CreateObject("Wscript.Shell")
  	set objFSO = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------------------------
	' Some Variables for Date, Year TOD
	D = Day(Date)
	Y = Year(Date)-2000
	T = Hour(Time) & Minute(Time)
	WScript.Echo(Y)
	WScript.Echo(T)

	Y = "Yeshai"
	T = "What???"
	WScript.Echo(Y)
	WScript.Echo(T)

So as you can see above I've used my Y and T variable's twice.... first time they had my Year and Time value and second time they got the values I assigned to them, Yeshai and What???
Here is my quick test output screen

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
11
2255
Yeshai
What???
***** script completed - exit code: 0 *****

Now in a real world you would write it like so:

	Option Explicit
'----------------------------------------------------------------------
' Yeshai Bouskila - Some Script
'----------------------------------------------------------------------
  	set objShell = CreateObject("Wscript.Shell")
  	set objFSO = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------------------------
	' Some Variables for Date, Year TOD
	Dim D, Y, T
	D = Day(Date)
	Y = Year(Date)-2000
	T = Hour(Time) & Minute(Time)
	WScript.Echo(Y)
	WScript.Echo(T)

	Dim Y,T
	Y = "Yeshai"
	T = "What???"
	WScript.Echo(Y)
	WScript.Echo(T)

And the error would be "Testing.vbs(19, 6) Microsoft VBScript compilation error: Name redefined" (From my output box of VBS Edit) but you would get the same error if you run the file.

So bottom line here do what ever you want to do, one persons best practice is not always the right way, an you don't have to listen to me as i'm not a real developer but do this both for fun and work....

Print This Post Print This Post
26Dec/100

VBScript – Using SUB’s and not the Sandwich and Functions

So, you have seen how do use some of my ideas (Well not only mine as you can find them across the WWW but this is my Blog so they are mine for this POST....)  and one of them was the WriteToLog Sub So let's talk about SUB's and Functions what we can do and use them for.

So what's the big idea? and whats the difference between a SUB and a Function?
Both the SUB and the Functions are Procedues that can be called within your script and execute some functionality without writing lines of code over and over....
There is one major difference between SUB's and Functions and it's that a Function can return a value and a SUB can only run the proceduer.
So just as an example my WriteToLog SUB is called from withing the script's and writes to a file based on arguments provided like Text, Time, File information ETC.
And as another Example that I will write about in a future date a Function that will return if  "RegistryItemExists" or "FileVersion" you catch my drift here...

A SUB (Again not the one from Subway) is a Subroutine or some might call it a procedure are basically an instance of code that you are going to use multiple times around your script but only right the main portion ounce.

So how do you start?
The Sub will Start with a Sub and a Value as well as some Arguments here is an example of a Copy SUB

Sub xCopyFile(xCopyFrom, xCopyTo)
----> Here goes the code
End Sub

So in this case I will write all my code inside the sub where xCopyFrom, xCopyTo will get values from the execution line in your code
So my execution line could look like

xCopyFile = "C:\Test.txt", "C:\Temp\Test.txt"

In this case
"C:\Test.txt" = "xCopyFrom" argument
"C:\Temp\Test.txt" = "xCopyTo" argument

And my real SUB could do some other things like verify that the destination

Now a Function
So a Function is writen in the same generic way to open and close the function


Function FileVersion(FileName)

-----> Code goes here

End Function

And in this case i'm going to get a file version for components I want to upgrade on the system and do something if needed.....
So the Code behind the function is simple as well (you do need some more code on your script...)


Function FileVersion(FileName)

FileVersion = objFSO.GetFileVersion(FileName)

End Function

So in this case from the script I will call the function in this way
MonitorFolderApp = FileVersion("C:\MonitorApp\MonitorApp.Exe")

So
MonitorFolderApp is the variable I want to assign the version to
= FileVersion calls the function
and ("what ever arguments", "Some More Arguments")  are the arguments you pass on to the Function

You might say why should I use a function just for one line of code? well you might be correct but here is an example for checking if a registry key exists as a function (I found this online somewhere, I will need to find where so I can give them some credit!)

Function xRegItemEX (xRegKey)
 On Error Resume Next

 If (Right(xRegKey, 1) = "\") Then
 objShell.RegRead xRegKey
 Select Case Err
 Case 0:
 xRegItemEX = true
 Case &h80070002:
 ErrDescription = Replace(Err.description, xRegKey, "")
 Err.clear
 objShell.RegRead "HKEY_ERROR\"
 If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
 xRegItemEX = true
 Else
 xRegItemEX = false
 End If
 Case Else:
 xRegItemEX = false
 End Select
 Else
 objShell.RegRead xRegKey

 Select Case Err
 Case 0:
 xRegItemEX = true
 Case Else
 xRegItemEX = false
 End Select
 End If
 On Error Goto 0
End Function
Print This Post Print This Post
12Nov/100

VBScript – Passing Arguments

So you want to pass some arguments to your VBScript either with a shortcut or command prompt...

Why do you need this at all? let's say I have some customer I want to deploy some software or do something on their system and as well all know (Or we might not... at least not me) there is no way to Compress/UnCompress (Zip'd) files via VBScript in a nice way.

Or I want to automate a procedure to upgrade and if needed do a downgrade.

So the idea here is simple, I create a VBScript that will preform an UnZip and I want to specify what file ti UnZip and where to extract it to (I know you can always use WinZip Self Extractor of some sort but this is just an example for Arguments).

So here goes, simple start of the script with some objects (if you want you can open explicits I don't think it's needed with this one)


set objShell = CreateObject("Wscript.Shell")

set objFSO = CreateObject("Scripting.FileSystemObject")

Next line is where we set to get arguments from the script


Set args = WScript.Arguments

Now i'm going to do some verification here that if no arguments are passed i'm going to show an error with a MSGBOX as you can see in the screenshot below, and when they hit OK i'll quit the script.


If Wscript.Arguments.Count = 0 Then

MsgBox ("Add some arugments this will not work without them...." &amp; vbNewLine &amp; _

"Please add MainFile and Locaion do unzip like Script.vbs C:\Temp\Temp.zip C:\Temp\")

WScript.Quit

Here is where i'm going to assign the arguments to a value, each arogument is passed as a number where the first argument is (0) and moving on.

In this case my command would be UnZip.vbs C:\Temp\Upgrade1234.zip C:\Temp\SoftwareUpgrade1234\

So

args.Item(0) = C:\Temp\Upgrade1234.zip  <-- First one

And

args.Item(1) =  C:\Temp\SoftwareUpgrade1234\ <-- Second one

I think you catch what i'm talking about...


Else

PathOfZipFile = args.Item(0)

ExtractZipTo = args.Item(1)

End If

And this is what I do to UnZip the files.... (simple but make sure that you create folders if needed ETC...)


set UnZipIt = CreateObject("Shell.Application")

set FilesInzip=UnZipIt.NameSpace(PathOfZipFile).items

UnZipIt.NameSpace(ExtractZipTo).CopyHere(FilesInzip)

So here is the whole code.... just so it's easy on the copy/paste (That you can't do on WP7... YET)


set objShell = CreateObject("Wscript.Shell")

set objFSO = CreateObject("Scripting.FileSystemObject")

Set args = WScript.Arguments

If Wscript.Arguments.Count = 0 Then

MsgBox ("Add some arugments this will not work without them...." &amp; vbNewLine &amp; _

"Please add MainFile and Locaion do unzip like Script.vbs C:\Temp\Temp.zip C:\Temp\")

WScript.Quit

Else

PathOfZipFile = args.Item(0)

ExtractZipTo = args.Item(1)

End If

set UnZipIt = CreateObject("Shell.Application")

set FilesInzip=UnZipIt.NameSpace(PathOfZipFile).items

UnZipIt.NameSpace(ExtractZipTo).CopyHere(FilesInzip)
Print This Post Print This Post
27Jul/100

VBSEdit and HTA Edit Quick Recommendation

VBSEdit what's it all about...
For the past two years I've been scripting for customers.
At first I used simple text editors as NotePad ++ or UltraEdit but I simply wanted much more and while reviewing more then one option on the market VBS Edit seemed like the perfect fit (price and value).
With built in debugger, EXE conversions, HTA Editor included in the package, great GUI and sample scripts all for the  small price of $59 you can't really go wrong here.
Click on read more for some screen shots and a review (not mine)

AderSoft VBS Edit website - www.vbsedit.com - There is a free version but the Debugger will increment a wait timer

Print This Post Print This Post