Sticky Posted November 17, 2010 Posted November 17, 2010 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. expandcollapse popup#include <Constants.au3> #include <Array.au3> If $CmdLine[0] > 0 Then _closeLockedFile($CmdLine[1]) Else MsgBox(0, "Error", "Must pass the file name to check as a parameter") EndIf 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 Wend $foundFiles = StringSplit($output, @CRLF) ;find beginning of file list While 1 If StringInStr($foundFiles[1], "---------------------------------------------------------------------") Then _ArrayDelete($foundFiles, 1) ExitLoop Else _ArrayDelete($foundFiles, 1) EndIf WEnd For $i = 1 To $foundFiles[0] If $i > UBound($foundFiles) - 1 Then _ArrayDelete($foundFiles, $i - 1) ExitLoop EndIf ;remove blank lines If $foundFiles[$i] = "" Then _ArrayDelete($foundFiles, $i) $i -= 1 EndIf Next $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 EndIf Next Return $suckersKilled EndFunc
arcker Posted November 18, 2010 Posted November 18, 2010 would be better to use udf instead of running commands. _Net_Share_FileClose($sServer, $iFileID) _Net_Share_FileEnum -- 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]
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now