Sign in to follow this  
Followers 0
jguinch

How to retrieve opened files

6 posts in this topic

Hi

Sorry for my bad english...

I have a scheduled task witch connects en disconnects network drives and executes some backup scripts, and sometime several opened files(handles) don't automaticaly close.

I'm looking for a way to list and close all files opened on a network path.

For exemple, if \\server\share\toto.exe or \\server\share\radomFile.doc is openned, i wait to force it to close

For processes, I can use ExecutablePath with WMI32_Process , but for opened files, i don't know.

Thanks for your help

Share this post


Link to post
Share on other sites



If you are backing up your local server you can run "net session" to get the computers that are connected to the local server and then parse the list and run "net session /delete" to close those sessions and any open files that they may have. Keep in mind, however, that this would not be a graceful close for those connected. Other than that I think you would have to check each file one by one with a script like to see if the file is currently open.

Share this post


Link to post
Share on other sites

Sysinternals had a command line utility I used "handle.exe" to do something similar ( unlock a closed file ).

There are NtQuery* dll functions that I think I tried to accomplish the same with that failed a while back.

Not sure how your network is setup, or how you're doing what you're doing, most I know use PsExec ( http://technet.microsoft.com/en-us/sysinternals/bb897553 ) to make/take their actions.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Thanks for your answer.

Your link is very interesting but it's not exactly what I'm looking (one by one files is not possible)

As I said then, use the built in net command ("net session") to get open sessions to your server and close the sessions. This can be catastrophic for clients running executables or Office documents. Have you looked into Volume Shadow Copy? Here is an example of closing the sessions.

;#include <Array.au3>   ;only needed for _ArrayDisplay
#include <File.au3>

Do
    Sleep(10)
Until _CloseOpenSessions() = 1

Func _CloseOpenSessions()
    Local $tFile = _TempFile()
    RunWait(@ComSpec & ' /c net session > "' & $tFile & '"', @SystemDir, @SW_HIDE)

    Local $String = FileRead($tFile)
    If Not $String Then Return
    Local $Servers = StringRegExp($String, '\\\\[^\s]+', 3)
    If IsArray($Servers) Then
        ;_ArrayDisplay($Servers, 'Servers with Open sessions')  ;uncomment this for testing
        For $Element In $Servers
            RunWait(@ComSpec & ' /c net session ' & $Element & ' /DELETE /Y', @SystemDir, @SW_HIDE)
        Next
    Else
        Return 1
    EndIf
EndFunc   ;==>_CloseOpenSessions

Share this post


Link to post
Share on other sites

Sysinternals had a command line utility I used "handle.exe" to do something similar ( unlock a closed file ).

There are NtQuery* dll functions that I think I tried to accomplish the same with that failed a while back.

Not sure how your network is setup, or how you're doing what you're doing, most I know use PsExec ( http://technet.microsoft.com/en-us/sysinternals/bb897553 ) to make/take their actions.

Hi

handle.exe seems to be a good solution for me, thanks !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0