A simple vbscript file for server to clear all opened file via network sharing
This is a vbscript for Windows Server (tested on 2003 R2) to clear any opened file before an ETL job. Any opened resource and file locking will cause the ETL job failure.
The original posting uses "Remove" method to remove any open resource. Unfortunately, the method does not exist and the script cannot run. Therefore, I make use of the good old "NET FILE" command to do the "remove" task.
' Added 2012-03-30 I just want to remove file locking for a specific folder
The original posting uses "Remove" method to remove any open resource. Unfortunately, the method does not exist and the script cannot run. Therefore, I make use of the good old "NET FILE" command to do the "remove" task.
Sample 1:
' Clear all opened file
' Created by: Stanley Tsui
' Created Date: 2011-10-11
' Reference:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/16/how-can-i-list-open-sessions-and-open-files-on-a-computer.aspx
' Revision History
' 2011-10-20
Stanley Add logging. Add logging of opened resources and sessions
' 2012-03-30 Stanley Change the default value of intLock to -1. It is because 0 is for read only locking. The -1 is to detect if the file lock object cannot be fetch correctly.
Set objConnection = GetObject("WinNT://server/LanmanServer")
Set colResources = objConnection.Resources
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile ("Z:\file_sharing_log.txt",
ForAppending, True)
strOutput =
""
strCommand
= ""
strResName
= ""
intLock = -1
strTime = "--------" & vbCrLf & "Now:
" & Now & vbCrLf
objTextFile.WriteLine(strTime)
objTextFile.WriteLine("[Sessions]" & vbCrLf)
Set colSessions = objConnection.Sessions
' -- Optional -- just to dump out the connected sessions
'For Each
objSession in colSessions
' strOutput = strOutput & "Computer:
" & objSession.Computer & vbCrLf
' strOutput = strOutput & "Connected
Time: " & objSession.ConnectTime & vbCrLf
' strOutput = strOutput & "Idle
Time: " & objSession.IdleTime & vbCrLf
' strOutput = strOutput & "Name:
" & objSession.Name & vbCrLf
' strOutput = strOutput & "User:
" & objSession.User & vbCrLf
' objTextFile.WriteLine(strOutput)
'Next
objTextFile.WriteLine("[Resources]" & vbCrLf)
For Each objResource in colResources
strOutput = ""
strResName = ""
intLock = -1
strCommand = ""
On Error Resume Next
intLock = objResource.LockCount
strResName = objResource.Name
strOutput = vbCrLf & "Resource Name: " & strResName & vbCrLf
strOutput = strOutput & "User
: " & objResource.User & vbCrLf
strOutput = strOutput & "Path
: " & objResource.Path & vbCrLf
strOutput = strOutput & "Lock count
: " & intLock
' strOutput = strOutput & vbCrLf
objTextFile.WriteLine(strOutput)
if StrComp(left(objResource.Path
, 25), "z:\myfolder") =
0 Then
'Wscript.Echo strOutput
'Wscript.Echo "Path: " &
objResource.Path
'Wscript.Echo "User: " &
objResource.User
'Wscript.Echo
if
intLock >= 0 Then
objTextFile.WriteLine(strOutput)
'colResources.Remove(strResName) -- net file is used instead
strCommand = "net file " & strResName & " /close"
objResult = objShell.Run(strCommand,
1, True)
objTextFile.WriteLine("-> cleared")
end if
end if
On Error Goto 0
Next
strTime =
"End at: " & Now
objTextFile.WriteLine(strTime)
objTextFile.Close
Comments