Search the Community
Showing results for tags 'net share'.
-
I have been working on a project, where part of the script updates a shared file on a server. Sometimes the file would be left open by an end user, causing FileCopy to fail when the file needed to be updated. I wrote the following functions to close the file on the server before coping it to the share. For the functions to run correctly, the script user has to be a member of the Administrators or Server Operators local group on the server where the share is located. These functions are based on the example in the Help File for _Net_Share_FileClose. #include <NetShare.au3> Global $sFile = ".\Test\test.txt" Global $sServerShare = "\\ServerName\Share\subfolder1\subfolder2" Global $sErrorMsg = "" _CopyFileToServerShare($sServerShare, $sFile) Switch @error Case 1 $sErrorMsg = "Invalid server path." Case 2 $sErrorMsg = "Invalid file path." Case 3 $sErrorMsg = "Unable to close remote open file." Case 4 $sErrorMsg = "Unable to write file to server share." Case Else EndSwitch If $sErrorMsg <> "" Then MsgBox($MB_ICONERROR, "Error", $sErrorMsg) Func _CopyFileToServerShare($sServerSharePath, $sFileNamePath) If $sServerSharePath = "" Or (Not FileExists($sFileNamePath)) Then Return SetError(1, 0, False) If $sFileNamePath = "" Or (Not FileExists($sFileNamePath)) Then Return SetError(2, 0, False) If StringRight($sServerSharePath, 1) = "\" Then $sServerSharePath = StringTrimRight($sServerSharePath, 1) _CloseOpenNetSharedFile(StringRegExpReplace($sServerSharePath, "(\\\\[^\\]*)\\.*", "$1"), StringRegExpReplace($sFileNamePath, "^.*\\", "")) If @error Then Return SetError(3, 0, False) Sleep(1000 * 10) ;Wait 10 seconds for the file to close. This can be changed to a faster time, it depends on server and network speed. If Not FileCopy($sFileNamePath, $sServerSharePath & "\", $FC_OVERWRITE) Then Return SetError(4, 0, False) Return True EndFunc Func _CloseOpenNetSharedFile($sServer, $sShareFileName) If StringLeft($sServer, 2) <> "\\" Then $sServer = "\\" & $sServer ; Enumerate open files on the server Local $aFiles = _Net_Share_FileEnum($sServer) If @error then Return SetError(1, @error, False) ; Force close any file open named $sShareFileName. Local $iError = 0 For $i = 1 To $aFiles[0][0] If StringInStr($aFiles[$i][3], $sShareFileName) Then If Not _Net_Share_FileClose($sServer, $aFiles[$i][0]) Then $iError += 1 EndIf Next If $iError Then SetError(2, $iError, False) Return True EndFunc Adam
-
- copy file
- close share file
-
(and 1 more)
Tagged with: