DX

Having a little problem with VB script. I've checked two sources fo VB script samples and no luck (including MS VB script reference) The code is:

("Sub Object_OnScriptEnter
Set filesys = CreateObject("Scripting.FileSystemObject")
Set drv = filesys.GetDrive("C:")
Set MyDrivestate = drv.IsReady
Object.SetTimer 12345, 500
End Sub
Sub Object_OnTimer12345
If MyDrivestate Then
Object.State = "Ready"
Else
Object.State = "Busy"
End If
End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
Object.KillTimer 12345
End Sub")

This bombs on line 4 where I attempt to Set MyDriveState it doesn't treat drv as an object. Trying to reference drv with DesktopX.Object("drv").IsReady doesn't work comes back invalid as does DesktopX.ScriptObject("drv").IsReady. So it seems the Drive object in VB script isn't working for me or I don't know DesktopX's syntax for this. Anyone know a solution for this?
1,996 views 1 replies
Reply #1 Top
I adapted the Get Free Space sample from over at w3Schools. It works for me.

Here is a sample where I successfully polled the free space on my C: drive.


Option Explicit
'Called when the script is executed
Sub Object_OnScriptEnter

End Sub

Sub Object_OnLButtonUp(x, y, d)
If d Then Exit Sub
Dim fs, drive, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set drive = fs.GetDrive("c:")
n = "Drive: " & drive & vbNewLine
n = n & "Available Space in bytes: " & drive.AvailableSpace
MsgBox n
Set fs = Nothing
Set drive = Nothing
End Sub

'Called when the script is terminated
Sub Object_OnScriptExit

End Sub



The line in your code, Set MyDrivestate = drv.IsReady troubles me. IsReady does not return an object that would require the use of the Set keyword. It is just a boolean property.


I wish we could easily post formatted code, to make it easier to read. But I think the problem lies not with getting the GetDrive to work, but with some internal logic. It looks like, from the script, you are setting a variable in the OnScriptEnter, and then checking the variable twice a second, which wouldn't achieve much.

Remember variable scopes, you must Dim outside of a sub or function if you want the variable to be accessible from all other subs and functions.

It appears that you are checking the drive status but once, then rechecking that initial check twice a second. You'll need to reperform the actual check on each firing.


Sub Object_OnTimer1134
Dim fs, drive
Set fs = CreateObject("Scripting.FileSystemObject")
Set drive = fs.GetDrive("c:")
If drive.IsReady Then
Object.State = "Ready"
Else
Object.State = "Not Ready"
End If
Set fs = Nothing
Set drive = Nothing
End Sub


ala This othe w3Schools sample