VBS to Close Open Network Shared file

Home Forums Scripting Windows Script Host VBS to Close Open Network Shared file

This topic contains 1 reply, has 2 voices, and was last updated by Avatar olaolaniyi 3 years, 12 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Avatar
    vane0326
    Member
    #165467

    Hi,

    I have this code below that will close all files in a particular SHARED Network folder. I would like to be modified to just close a particular file only.

    Code:
    strComputer = “file-server”
    strFolder = “G:HR”

    On Error Resume Next

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objShell = WScript.CreateObject(“WScript.Shell”)

    Set objConnection = GetObject(“WinNT://” & strComputer & “/LanmanServer”)
    Set colResources = objConnection.Resources

    For Each objResource in colResources
    strParent = objFSO.GetParentFolderName(objResource.Path)

    If LCase(strParent) = LCase(strFolder) Then
    objShell.Run “openfiles /disconnect /s ” & strComputer & ” /id ” &_
    objResource.Name, 0, False
    End If
    Next

    Avatar
    olaolaniyi
    Member
    #390866

    Hi,

    I found this code below an it shows to work great, BUT sometimes it will not work. I’m thinking the file might be cache on the user computer. Maybe there’s a work around. Will continue to research on this.

    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 = “file-server”
    strFileToClose = “LIST.pdf”
    strPSExecPath = “\file-serverShortcutspsexec.exe”

    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 = strPSExecPath & ” -accepteula -e ” & strServerName & ” cmd /c Net Files ” & strFileID & ” /close”
    objShell.Run strCommand, 0, True
    WScript.Echo strFileID & ” has been closed.”
    Next
    ‘WScript.Echo “Which ID do you want to close? ”
    ‘strFileID = WScript.StdIn.ReadLine
    ‘If Trim(strFileID) “” Then
    ‘ strCommand = strPSExecPath & ” -accepteula -e ” & strServerName & ” cmd /c Net Files ” & strFileID & ” /close”
    ‘ ‘InputBox “About to run:”, “Command to Run”, strCommand
    ‘ objShell.Run strCommand, 0, True
    ‘ WScript.Echo strFileID & ” has been closed.”
    ‘End If
    Else
    WScript.Echo “No matching open files were found.”
    End If
    ‘====================

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.