I came across an oddity whilst developing some VB script code as a training exercise. I define and use a variable to store a copy of the original file and folder selections in order to restore them after I am done. The relevant statement is..
Set RememberSelected = cmd.files ' Remember initial selections
As long as an active statement which references the object variable (e.g)..
n = RememberSelected.count
..is present then the code inside the With .. End With block near the end correctly restores the original file and folder selections. However, if I comment out the n = RememberSelected.count statement before executing the With .. End With code, then original selections are not restored.
It looks like the object is undefined until it is referenced.
[code]' VBScript Smart front end to Opus GETSIZES command
' OnInit is called by Directory Opus to initialize the script
Function OnInit(initData)
initData.name = "Smart GETSIZES"
initData.desc = "Smart front end to Opus GETSIZES command"
initData.copyright = "(c) 2014 AussieBoykie"
initData.default_enable = true
Set cmd = initData.AddCommand
cmd.name = "$GetSizes"
cmd.method = "GetFolderSizes"
cmd.desc = ""
cmd.label = cmd.name
cmd.template = ""
End Function
' This is where the "smarts" are
Function GetFolderSizes(ByRef fd)
Dim cmd
Dim i
Dim n
Dim sTab
Set sTab = fd.func.sourcetab
If Not sTab.dirs.count > 0 Then Exit Function ' Nothing to do
Set cmd = fd.func.command
cmd.deselect = false ' Do not deselect any selected files and folders
Dim RememberSelected
Set RememberSelected = cmd.files ' Remember initial selections
n = RememberSelected.count ' (Beta9) Apparently need to reference RememberSelected
n = sTab.selected_dirs.count ' Initially we are only concerned with selected folders
cmd.clearfiles ' Clear the list of files and folders to be operated on
For i = 0 To n-1
If sTab.selected_dirs(i).got_size = false Then ' If any selected folder is not sized
cmd.addfile sTab.selected_dirs(i) ' then add it to the list of files and folders to be operated on
End If
Next
If cmd.files.count = 0 Then ' There were no unsized selected folders
n = sTab.dirs.count ' Now we are interested in all folders
For i = 0 To n-1
If sTab.dirs(i).got_size = false Then ' If any folder is not sized
cmd.addfile sTab.dirs(i) ' then add it to the list of files and folders to be operated on
End If
Next
End If
If cmd.files.count > 0 Then ' Some folder sizing to be done
With cmd
.clear
.addline "Select NONE"
.addline "Select FROMSCRIPT"
.addline "GETSIZES"
.run
.clear
.addline "Select NONE"
.addline "Select FROMSCRIPT"
.setfiles RememberSelected
.run
End With
End If
End Function[/code]
Regards, AB