I am trying to write VBScript that will check for an open file on our file share then close the session to the file if found. The problem is that the object's session ID comes back negative sometimes, so it does not always work. When comparing it to the actual session ID using Net File, it is completely different. Below is the code.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
intFound = 0
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strDomain = objNetwork.UserDomain
strServerName = "myServerName"
strFileToClose = "Some Open File.PDF"
Set objConnection = GetObject("WinNT://" & strDomain & "/" & strServerName & "/LanmanServer")
Set objOpenFiles = objConnection.Resources
WScript.Echo "Files open on " & strServerName & VbCrLf & "=============================="
strIDs = ""
For Each objFile In objOpenFiles
On Error Resume Next
temp = objFile.User
If Err.Number = 0 Then
On Error GoTo 0
If InStr(LCase(objFile.Path), LCase(strFileToClose)) > 0 Then
WScript.Echo objFile.Name & " || " & objFile.Path & " || " & objFile.User
If strIDs = "" Then
strIDs = objFile.Name
Else
strIDs = strIDs & ";" & objFile.Name
End If
intFound = intFound + 1
End If
Else
Err.Clear
On Error GoTo 0
End If
Next
WScript.Echo ""
If intFound > 0 Then
arrIDs = Split(strIDs, ";")
For Each strFileID In arrIDs
strCommand = "cmd /c Net File " & strFileID & " /close"
objShell.Run strCommand, 0, True
WScript.Echo strFileID & " has been closed."
Next
Else
WScript.Echo "No matching open files were found."
End If
Here is an example of the output with a negative session ID from the script then the actual results from Net File. Keep in mind that some of the time they match and other times they do not!
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Files open on myServerName
==============================
-2013150206 || C:\\Users\user\Desktop\Some Open File.PDF || user
-2013150206 has been closed. 'Not true since it is a negative number
C:\Users\admin\Desktop>net file 2281817090 'Actual Session ID
File ID 2281817090
User name user
Locks 0
Path C:\\Users\user\Desktop\Some Open File.PDF
Permissions R
The command completed successfully.
C:\Users\admin\Desktop>
What I believe is going on is that the variant's datatype is getting changed in that if the number is big enough it will get stored as a negative value... I do not think you can change it to an unsigned integer, and I could be completely wrong in assuming I even know what is going on.