Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Denied on fso.CopyFile

Status
Not open for further replies.

draigGoch

Programmer
Feb 10, 2005
166
GB
I am programming a script that will backup files from the server onto another computer on the network - simple stuff really (well, it was supposed to be!).

I am copying a shared folder off the server by running my own function to copy it - e.g.

call myCopyFunction ("\\server\folder", "c:\backup")

I was getting an error message - Permission Denied on the copyfile lines so i thought it was a one-off with a file, so I added an On error resume next. Problem is, there must be an error with copying every file, because no file is copied, but the dir tree is copied.

The permissions on the server seem to be ok - Everyone is Full! I can copy files manually from that folder to my pc. Any ideas? Here's the function:

function copioffolder(gwreiddiol, newydd)
set ffolderGwreiddiol = fso.GetFolder(gwreiddiol)
if fso.FolderExists(newydd) then
else
fso.CreateFolder (newydd)
end if

for each ffeil in ffolderGwreiddiol.Files
fso.copyFile ffeil.path, newydd

if fso.fileExsists(newydd & "\" & ffeil.name) then
set ffeilgwreiddiol = fso.GetFile(ffeil.path)
set ffeilnewydd = fso.GetFile(newydd & "\" & ffeil.name)

if ffeilgwreiddiol.size <> ffeilnewydd.size then
'on error resume next
fso.copyFile ffeil.path, newydd
if ffeilgwreiddiol.size <> ffeilnewydd.size then
'A prob happened objXL.Cells(NiferOFfolderi+13+countErrors, 2).Value = ffeil.path
countErrors = countErrors+1
else
'file copied ok
end if
else
'file copied ok
end if
else
'A prob happened
objXL.Cells(NiferOFfolderi+13+countErrors, 2).Value = ffeil.path
countErrors = countErrors+1
end if
next

for each isffolder in ffolderGwreiddiol.SubFolders
call copioffolder (isffolder.path, newydd & "\" & isffolder.name)
next
end function

Sorry about var names - Welsh i'm afraid!

Thanks in advance

A computer always does what you tell it to, but rarely does what you want it to.....
 
for each ffeil in ffolderGwreiddiol.Files
fso.copyFile ffeil.path, newydd & "\", True


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
[1] Why not check out copyfolder method to simply thing?

[2] As to the existing script itself.

[2.1] Typos?
>[tt]if fso.fileEx[highlight]s[/highlight]ists(newydd & "\" & ffeil.name) then[/tt]
[tt]if fso.fileExists(newydd & "\" & ffeil.name) then[/tt]
(I hope it is not localized method name.)

[2.2] This is the main problem.
>[tt] fso.copyFile ffeil.path, newydd[/tt]
[tt] fso.copyFile ffeil.path, newydd [red]& "\"[/red][/tt]

[2.3] This may be just editing mishap. Newline is needed.
>'A prob happened [highlight] [/highlight]objXL.Cells(NiferOFfolderi+13+countErrors, 2).Value = ffeil.path

[3] Do you have reason you fear that the file not being copied? Even so, you should leave sufficient time lapse for the copy to complete. Hence, the structure like this
[tt]>> fso.copyFile ffeil.path, newydd & "\"
>> if fso.fileExists(newydd & "\" & ffeil.name) then[/tt]
is problematic. I would rather have a loop to ascertain it like this.

[tt]fso.copyFile ffeil.path, newydd & "\", true
do while not fso.fileexists(newydd & "\" & ffeil.name)
wscript.sleep 200
loop
if fso.fileExists(newydd & "\" & ffeil.name) then[/tt]

But then the nonexistence part (else) would/might never be active. This embarrassing feature is not very attractive and calling for better re-scripting.

There are things that I don't quite like and that I don't look into; but each has his way of doing thing.
 
Thanks to both of you - it was the trailing slash that let me down!

Tsuji - wow! Thanks for the help with the typo and 3rd point. Will implement this.

There were two reasons that I didn't use the CopyFolder method:

1) When I used the copyFolder, it wouldn't let me copy a top level network share folder for some bizzare reason, but if i chose another one, it worked fine!

2) Also with the copyFolder does it not escape the copy if it encounters an error with any file?

Thanks again for your help. Now to tidy up the rest of the code.........

A computer always does what you tell it to, but rarely does what you want it to.....
 
you do a

if fso.FolderExists(newydd) then
else
fso.CreateFolder (newydd)
end if

i think you might find that if you pass if c:\backup\hello
it will fall over on the fso.CreateFolder due to backup and hello not existing...

if this is the case then you might want to consider a
aArray = Split(newydd, "\")
For Each aElement In aArray
If Not FSO.FolderExist(aElement ....

this is psuedo code of course but i hope it illustrates the point?
 
Thanks for the post mrmovie, but the root folder is created elsewhere in the code and passed in - and each subfolder in turn, is created each time the function is called.

I'm having some trouble implementing the do while loop that tsuji suggested:

do while not fso.fileexists(newydd & "\" & ffeil.name)
wscript.sleep 200
loop

but one of the files will not copy for some reason - and this loop therefore goes on indefinately. Any suggestions?

A computer always does what you tell it to, but rarely does what you want it to.....
 
Add a counter to limit the number of sleep called.

But, that is always a case you should look into. Either destination already exists and is readonly---then you should delete the destination or change the readonly attribute. If for some other reason, you have to study it why.
 
with regards to the loop and sleep i have never come across a need for one with regards to a file copy.
personally i would avoid a Sleep in a script like the plague and even more so a Loop which contains one

i know i am on a soap box mission this week but.,.

by the nature of what a Function is the ultimate goal is that it can be re-used again and again in other scripts etc if needed. therefore i would say it would be good programming practice to make sure it can handle any length of folder depth so to speak.

regards
richard
 
>with regards to the loop and sleep i have never come across a need for one with regards to a file copy.
You will get fso.fileexists() return 0 if you try. It is done asynchronously.
 
sorry tsuji but i beg to differ

Set FSO = CreateObject("Scripting.FileSystemObject")FSO.CopyFile "c:\test.exe", "h:\"
Msgbox FSO.FileExists("h:\test.exe")

test.exe is 20meg and my H drive is a USB memory bird.
If what you are saying is correct (unless i am being a plum) then i should get the Msgbox almost immeadiately?

On my machine i get the Msgbox about 15 seconds after i start the script and about the same time as it appears in the H drive in the GUI
 
mrmovie, it is I who feel sorry. A quick test on this does reveal .fileexists() can be determined probably from the memory(?), even the same for the file.size data. So you're right. My apology! I take back the observation.

It is the attempt to open it (for instance, using .run "notepad " & file.path) which poses problem. That I have to wait (sleep). Hence, the surrender of the control to the next script line is done according to the agenda of fso, not physically written to the disk. So I would as well have difficulty to qualify it as synchronously.

Thanks again mrmovie for the counter-opinion which is correct is the context under scrutiny.
 
no need for the apology, you did have my panicking about some of my scripts though ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top