Jump to content
Sign in to follow this  

Close Open and Locked File on Server

Recommended Posts


I have a script that constantly writes to a text file on a server share which needs to remain unlocked. Every once in a while, it would end up locked so I needed a way to unlock it. I used the following line to kick off a scheduled task on the server hosting the share and file:

Run(@ComSpec & " /c "&'C:\Windows\System32\Schtasks.exe /Run /s SERVERNAME /tn "TASKNAME"', @SystemDir, @SW_HIDE)

The below script is compiled and has its own scheduled task assigned with the filename being passed as a parameter. Tested and working on XP SP3 and Win Server 2003.

#include <Constants.au3>
#include <Array.au3>

If $CmdLine[0] > 0 Then
    MsgBox(0, "Error", "Must pass the file name to check as a parameter")

Func _closeLockedFile($file)
    Local $foo = Run(@ComSpec & " /c net file", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $line, $output = ""

    While 1
        $line = StdoutRead($foo)
        If @error Then ExitLoop
        $output &= $line

    $foundFiles = StringSplit($output, @CRLF)

    ;find beginning of file list
    While 1
        If StringInStr($foundFiles[1], "---------------------------------------------------------------------") Then
            _ArrayDelete($foundFiles, 1)
            _ArrayDelete($foundFiles, 1)

    For $i = 1 To $foundFiles[0]
        If $i > UBound($foundFiles) - 1 Then
            _ArrayDelete($foundFiles, $i - 1)

        ;remove blank lines
        If $foundFiles[$i] = "" Then
            _ArrayDelete($foundFiles, $i)
            $i -= 1
    $foundFiles[0] = UBound($foundFiles) - 1

    $suckersKilled = 0
    For $f = 1 To $foundFiles[0]
        $fileId = StringMid($foundFiles[$f], 1, 11)
        $filePath = StringMid($foundFiles[$f], 12, 40)
        $fileLocks = StringMid($foundFiles[$f], 73, 5)

        $fileId = StringStripWS(StringStripWS($fileId, 1), 2)
        $filePath = StringStripWS(StringStripWS($filePath, 1), 2)
        $fileLocks = Int(StringStripWS(StringStripWS($fileLocks, 1), 2))

        If StringRight($filePath, Stringlen($file)) = $file And $fileLocks > 0 Then
            ;kill the sucker!
            Run(@ComSpec & " /c net file "&$fileId&" /close", @SystemDir, @SW_HIDE)
            $suckersKilled += 1

    Return $suckersKilled

Share this post

Link to post
Share on other sites

would be better to use udf instead of running commands.

_Net_Share_FileClose($sServer, $iFileID)


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.