Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Permission Denied on fso.CopyFile

draigGoch (Programmer) (OP)
6 Oct 05 7:15
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.....

PHV (MIS)
6 Oct 05 8:03
    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

tsuji (TechnicalUser)
6 Oct 05 8:06
[1] Why not check out copyfolder method to simply thing?

[2] As to the existing script itself.

[2.1] Typos?
>if fso.fileExsists(newydd & "\" & ffeil.name) then
if fso.fileExists(newydd & "\" & ffeil.name) then
(I hope it is not localized method name.)

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

[2.3] This may be just editing mishap. Newline is needed.
>'A prob happened                    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
>> fso.copyFile ffeil.path, newydd & "\"
>> if fso.fileExists(newydd & "\" & ffeil.name) then

is problematic. I would rather have a loop to ascertain it like this.

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


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.
draigGoch (Programmer) (OP)
6 Oct 05 8:16
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.....

mrmovie (TechnicalUser)
6 Oct 05 8:28
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?
draigGoch (Programmer) (OP)
6 Oct 05 8:39
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.....

tsuji (TechnicalUser)
6 Oct 05 8:43
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.
mrmovie (TechnicalUser)
6 Oct 05 10:11
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
tsuji (TechnicalUser)
6 Oct 05 10:38
>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.
mrmovie (TechnicalUser)
6 Oct 05 11:09
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
tsuji (TechnicalUser)
6 Oct 05 11:48
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.
mrmovie (TechnicalUser)
6 Oct 05 12:12
no need for the apology, you did have my panicking about some of my scripts though

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close