Link to home
Start Free TrialLog in
Avatar of Patrick Miller
Patrick MillerFlag for United States of America

asked on

Copying files from one pc to another with CopyFile

I am copying files with this script and I am getting an error

Permission denied

Here is the script.

Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.CopyFile "c:\Program Files\ADT\edge\*.*", "\\cw10072\c$\Program Files\ADT\edge\"

I can copy files from my pc to the cw10072 pc through Windows but I get the Permission error when I try to run this script.  What am I doing wrong.

I also took the   Copy "c:\Program Files\ADT\edge\*.*", "\\cw10072\c$\Program Files\ADT\edge\"  in the command prompt and it worked fine.
Avatar of aelatik
aelatik
Flag of Netherlands image

With FSO *.* does not work, you will have to do it file by file or use CopyFolder method :

Dim objFSO, objFolder, objFiles, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("c:\Program Files\ADT\edge\")
Set objFiles = objFolder.Files

For Each objFile In objFiles
    objFSO.CopyFile objFile.Path, "\\cw10072\c$\Program Files\ADT\edge\", True
Next

Set objFSO = Nothing
Set objFolder = Nothing
Set objFiles = Nothing





For copyFolder use :

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "c:\Program Files\ADT\edge\", "\\cw10072\c$\Program Files\ADT\", True ' True = Overwrite
Set objFSO = Nothing


Avatar of Patrick Miller

ASKER

I tried using the CopyFolder and it comes up with an error.  

Path not found.
I tried using the copyfile option and it runs for a bit then I get the pop-up error      Permission denied.   I am admin with full rights so I am not sure why this is happening.
Hello Patrick

what r the OS involved in this copying?? if u r using NT,W2K or XP or Win2003 ur program will not be allowed access to copy/paste file between systems u need to use a API - WNetAddConnection2 to establish a connection between the computers before attempting to use FSO.CopyFile, there is no problem in copying files on network it works fantastic.

My code is too long to paste it here, but u can find example of using above API on net easily.

all the best
Do you have a good place you would recommend for examples of API on the web?
ASKER CERTIFIED SOLUTION
Avatar of sunil_mails
sunil_mails
Flag of India 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
here are the declarations for above api

Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUsername As String, ByVal dwFlags As Long) As Long
Avatar of TFGreen
TFGreen

aelatik,

Actually, you can use wildcards when using FSO to copy files.  I have attached an exaple from code that I have written that combines the wildcard with other characters.  What I use this for is to copy certain files to removable disk drives based on that drive's disk label (as opposed to its drive letter).

In the example, my variable is w and the script copies all files using both that and the wildcard.  So if the variable w was assigned a text string of "Week1" the command (in DOS) would look like

Copy   V:\A*Week1.*   (to destination)


objFSO.CopyFile "V:\A*"&w&".*" , objItem.DriveLetter, OverwriteExisting

Open in new window