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”)
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