The VBScript Network and Systems Administrator's Cafe:

VBScipt

May 2 2009   6:19PM GMT

Using the VBScript datediff function to determine the age of a file by the last modified and the last accessed time



Posted by: Jerry Lees
VBScipt, VBScript Functions, file access, file modified, modification date, System Administration, Disk space, disk utilities

I recently needed a way to tell if a file had been accessed or modified recently, something that has always been a question when you’re out of space on a server and can’t just add more space to it. What do you delete??? The Old files are the obvious answer… except if people are using them.

Here is teh script I wrote to tell if a file had been accessed or modified in the last X days. In the example I use 5 days, but you can use a different number of days when you call the function.

Enjoy!

Option Explicit

Dim fName
Const DateLastModified = 1, DateLastAccessed = 2

fName=”c:\temp2.txt”

if FileAge(fname,5,DateLastAccessed) = True Then
WScript.Echo (”The file was accessed recently enough!”)
Else
WScript.Echo (”The file was not accessed recently enough!”)
End If

if FileAge(fname,5,DateLastModified) = True Then
WScript.Echo (”The file was modified recently!”)
Else
WScript.Echo (”The file was not modified recently!”)
End If

Function FileAge(fName,fAge, CompareType)
‘function returns True if the file is older than the fAge (File Age) specified and false if it isn’t
Dim LastModified, LastAccessed, FSO, DateDifference

Set FSO = CreateObject(”Scripting.FileSystemObject”)
LastModified = FSO.GetFile(fname).DateLastModified
LastAccessed = FSO.GetFile(fname).DateLastAccessed

Select Case CompareType
Case 1
DateDifference = DateDiff(”n”,LastModified, Now())
Case 2
DateDifference = DateDiff(”n”,LastAccessed, Now())

End Select
If DateDifference > fage Then
FileAge = False
Else
FileAge = True
End If
End Function

Mar 10 2009   2:01AM GMT

Writing scripts that use speech with the Windows Speech API



Posted by: Jerry Lees
undocumented windows, SAPI.SpVoice, Sound, Speech API, Office Agent, VBScipt, VBScript Objects

I’ve been looking for ways to make a script speak the information it needs to relay to the user just for fun and, unfortunately the only scripts I could find out there were extremely elaborate onces that use the Microsoft Office Agents. While they fit the bill they were annoying and required Microsoft office components to be installed… that just wouldn’t do.

Then I found the Windows Sound API. It doesn’t require anything at all to be installed and it works fine. The script below is simple and easy to implement in your code. Check it out:

strText = “Hello, there…. can you write code?”

Set objVoice = CreateObject(”SAPI.SpVoice”)
objVoice.Speak strText


Feb 20 2009   3:53PM GMT

Using the IIS ADSI object to retrieve the anonymous user password for a server via VBScript



Posted by: Jerry Lees
iis, System Administration, Systems Administration, Toolkit, VBScipt, VBScript Functions, VBScript, working with objects

I recently had to change the anonymous user account for a change request for a site in IIS, but unfortunately the change did not work and we had to roll it back. However, I didn’t have and wouldn’t have known the account’s password since IIS and windows changes it periodically.

So, out came the scripting tool belt and I found that I had a script written previously that let me get the anonymous user account and password through the IIS ADSI object.

Below is a snippet from the script to display the password.

Function Get_IUSR_Password(ServerName)
   Dim IIsObject
   
   Set IIsObject = GetObject (”IIS://” & ServerName & “/w3svc”)
   on Error resume Next
   Get_IUSR_Password = IIsObject.Get(”AnonymousUserPass”)
   On Error GoTo 0
End Function

Enjoy!


Feb 13 2009   6:40PM GMT

Creating a unique GUID for use in your VBScripts



Posted by: Jerry Lees
GUID, Scriptlet.TypeLib, VBScipt

Today I answered a question in the ITKE unanswered questions for Word VBA on how to be certain a filename was unique across several users using it. The answer I gave answered the question, but I knew there was a way to create GUID’s within VBScript but couldn’t recall how.

GUID’s are cryptic to look at, but they are pretty much guaranteed to be unique. After some research I found the library that provides this functionality… Scriptlet.TypeLib! Below is a short script that gives you an idea of how to use it in your scripts to generate unique strings for when you need a guaranteed unique name or identifier.

Option Explicit

Dim GUID
Set TypeLib = CreateObject(”Scriptlet.TypeLib”)
GUID = TypeLib.Guid

Enjoy!


Feb 10 2009   6:11PM GMT

Easily compare dates from with in VBScript



Posted by: Jerry Lees
VBScipt, DATEDIFF, date, Dates, Date Comparison, VBScript Functions

On occasion you need to compare two dates in your scripts, this can be a real challenge when you approach the date as if it were a string and/or compare the date parts to one another. Then finding the difference can be a whole other set of challenges if your dates span several months, days, or years.

Luckily, there is a function right into VBScript that helps you compare two dates to one another and returns the difference in a unit of time that you specify! That function is the datediff function!

Below is an example that returns the number of seconds, minutes, hours, days, and years between now and February 25, 1973.

Option Explicit

Dim DateThen, DateNow

DateThen = “2/25/1973″
DateNow = Now

WScript.Echo DateDiff(”s”,DateThen, DateNow)
WScript.Echo DateDiff(”n”,DateThen, DateNow)
WScript.Echo DateDiff(”h”,DateThen, DateNow)
WScript.Echo DateDiff(”d”,DateThen, DateNow)
WScript.Echo DateDiff(”yyyy”,DateThen, DateNow)

Enjoy!


Dec 29 2008   3:21PM GMT

Using special characters in VBScript strings



Posted by: Jerry Lees
strings, String manipulation, string, working with variables, Special Characters, ASCII, VBScipt

I just realized I’ve been using Visual Basic String constants in my scripts with little to no explanation. We’ll rectify that situation right now! ;-)

When dealing with strings in VBScript, you occasionally need to add formatting characters to a string, so that when it is displayed on the screen or in a file it looks correct. If you know your ASCII chart you can certainly preform this with a “& chr(X) &”.However, if you don’t know what ASCII value a tab is (it’s 9) then you can simply use Visual Basic’s string constants in your scripts and have the added benefit if making it easier to read!

The constants and their meanings are listed below:

VBTab A Tab character [Chr(9)]
VBCr A carriage return [Chr(13)]
VBCrLf A carriage return and line feed [Chr(13) + Chr(10)]
vbBack A backspace character [Chr(8)]
vbLf A linefeed [Chr(10)]
vbNewLine A platform-specific new line character, either [Chr(13) + Chr(10)] or [Chr(13)]
vbNullChar A null character of value 0 [Chr(0)]
vbNullString A string of value 0 [no Chr code]; note that this is not the same as “”