Link to home
Start Free TrialLog in
Avatar of drschank
drschankFlag for United States of America

asked on

Copy file to network path using FSO.CopyFile

I am trying to copy a file to another server using FSO.CopyFile but I get a permission error.  I can copy the file with windows explorer without a problem but not with the API.

I found another question on EE with the same problem and followed the steps outlined in the accepted answer offered by expert sunil_mails, but it doesn't work for me.  Rather than repeat information here, the following is a link to that resolution:

https://www.experts-exchange.com/questions/21279060/Copying-files-from-one-pc-to-another-with-CopyFile.html

If I set the NetR.lpRemoteName = "\\server" I get no error on the WNetAddConnection2 but still get the permission error.  If I set it to "\\server\" I get the "Network path not found" error.  If I add a subpath to it ("\\server\path"), I get the "The credentials supplied conflict with an existing set of credentials." error.

Can anyone offer any help?

Thanks,
Dennis
Avatar of PaulHews
PaulHews
Flag of Canada image

How about using:

FileCopy "C:\directory\filename.dat", "\\server\path\filename.dat"

Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

'Your best bet would be to use API FileCopy.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long


Call CopyFile("c:\eula.txt", "\\server\path\eula.txt", 0)
You also might want to make sure you have the correct file permissons set either on the file, or on the network share
Try this
'==========================================
Private Sub Command1_Click()
'Add reference
'Project Menu -> References -> Microsoft Scripting Runtime
'---------------------------------------------------------
On Error GoTo ErrTrack
Dim objFso As New FileSystemObject

 Call objFso.CopyFile("Source_File", "\\Machiner_Name\Destination_Pat\NewFileName", True)
 MsgBox "File Copied", vbInformation
ErrTrack:
    Set objFso = Nothing
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Err.Clear
    End If
End Sub
'==============================

oops , correction

>>Machiner_Name
i mean Machine_Name

;-)
Shiju
Avatar of drschank

ASKER

The suggestion that shijusn offered is exactly what I have been trying to do but Err.Number returns 70 (Permission denied).  I tried using FileCopy command and the CopyFile API offered by PaulHews and egl1044, but got the "Permission denied" on FileCopy and "Access denied" on CopyFile API.

The file being copied does not yet exist in the destination path.  I can open Windows Explorer and drag-and-drop the file right into the directory without any problem but I cannot get it to copy through VB.  Are there different permissions to be considered when using VB vs. Windows Explorer?

Thanks,
Dennis
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial