Copy file to network path using FSO.CopyFile

Posted on 2005-03-14
Medium Priority
Last Modified: 2008-01-09
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:


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?

Question by:drschank
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 38

Expert Comment

ID: 13540409
How about using:

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

LVL 29

Expert Comment

ID: 13541511
'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)
LVL 29

Expert Comment

ID: 13541517
You also might want to make sure you have the correct file permissons set either on the file, or on the network share
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13542302
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
    Set objFso = Nothing
    If Err.Number <> 0 Then
        MsgBox Err.Description
    End If
End Sub

LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13542310
oops , correction

i mean Machine_Name


Author Comment

ID: 13545143
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?

LVL 38

Accepted Solution

PaulHews earned 2000 total points
ID: 13545340
If running in the IDE, it should be the same permissions as the logged in user.  If running under IIS or task manager or other process, then different permissions apply to those contexts.

If the file is open, you will definitely have these kinds of problems.  The only way to do it would be to use Explorer's own copying method:

Option Explicit

   hwnd As Long
   wFunc As Long
   pFrom As String
   pTo As String
   fFlags As Integer
   fAnyOperationsAborted As Boolean
   hNameMappings As Long
   lpszProgressTitle As String
End Type

Private Const FO_COPY = &H2
Private Const FOF_SIMPLEPROGRESS = &H100
Private Const FOF_NOCONFIRMMKDIR = &H200

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Command1_Click()
    Debug.Print fSHFileCopy("C:\test\openfile.csv", "C:\temp\test2.csv")
End Sub

Public Function fSHFileCopy(ByVal strSrc As String, ByVal strDst As String) As Boolean
    Dim lngRes As Integer
    fo.pFrom = strSrc
    fo.pTo = strDst
    fo.wFunc = FO_COPY
    lngRes = SHFileOperation(fo)
    fSHFileCopy = (lngRes = 0)  'return true if zero
End Function


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question