Jump to content
Sign in to follow this  

Copy a File to a Server Share

Recommended Posts


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 
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

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
    If $iError Then SetError(2, $iError, False)

    Return True




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.