Getting error while trying to copy file to network folder using FileCopy command

bfuchs
bfuchs used Ask the Experts™
on
Hi Experts,

I'm trying to use FileCopy command to copy a file from a local folder to a network location.
When I copy manually from the windows explorer it works fine.
However when I want use it thru FileCopy command I get the attached error

Assuming this must be given special permissions, any suggestions?
Untitled.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
Unhandles space in the name of a folder where you are copying from/to.

...
AlanConsultant

Commented:
Hi,

To follow on from Arnold's post above, if that is the issue, try wrapping the path / filename in quotes something like this:

"C:\Temp\Use This Folder\Filename.txt"


Alan.
Shaun VermaakSenior Consultant
Awarded 2017
Distinguished Expert 2018

Commented:
Could also be that file is locked, path too long errors. That error can mean a bunch of things
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
Oddly enough, ..this error also occurs if you are trying to over-write a read only file.
If so, you may wish to try renaming the (destination/network) file.

For example:
FileCopy "C:\YourSourceLocalFolder\test.txt", "h:\YourDestinationNetworkFolder\test0.txt"

...or see if you would be allowed to change the read only status of the destination file...

JeffCoachman
Hi Experts,

This is the command I'm trying to execute, has wrapping & no spaces, long path or read only issues..

FileCopy "C:\Users\bfuchs\Downloads\Tables_2017-Nov-05_2053.zip","E:\CaspioBackups"

Open in new window


Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

Are you running this from a user (with credentials) that have sufficient access to the files / folders in question?

If you aren't sure, try running it as a domain admin, and see if that works.  If so, then it tells you there is a probably a permissions issue, in which case you need to narrow it down and give the actual username that will run this the required permissions.

Thanks,

Alan.
@Alan,

Okay from DOS I can run it even w/o admin privileges.
Same is if I do it manually from win explorer.

So how come cant I do it from Access?
Any workaround?

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

That is odd.  Did you try running it from Access, but from a domain admin user login?

If that works, it still implies a permissions issue.

Is your Access code using credentials other than the user that ran Access at all?

Alan.
Well Everything is being done as same user login, which is my admin user at my own pc..
Do you think I must use shell to execute a DOS command within Access?

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

That would be the next thing to try - at least you know that it works from there (if you run it manually).

I don't see why that would work, whereas VBA won't, but maybe best to focus on getting it working any which way, then you can come back to it later if / when you have the time.


Alan.
Okay, lets try that.
Would you know the syntax?

Is this correct?
Shell("copy E:\test\y.txt E:\test\x.txt")

Open in new window


Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

I have never actually tried it in anything other than Excel, but I would guess it will be the same or similar:

Option Explicit

Sub Test()

Dim ReturnValue as Variant

ReturnValue = Shell("Copy C:\Users\bfuchs\Downloads\Tables_2017-Nov-05_2053.zip E:\CaspioBackups\")

End Sub

Open in new window



However, its been a very long time since I did that - maybe not even this century - so I might be out of date!

Alan.
I get File not found msg, see attached.
But it does exists..
See second attachment.

Thanks,
Ben
Untitled.png
Capture.PNG
AlanConsultant

Commented:
Hi Ben,

The error seems to be specific, but can you amend the code to try copying a 'test.txt' file from, say, C:\Temp\Test1\ to, say, C:\Temp\Test2\ to test that it works at all.

Thanks,

Alan.
Hi

Get same for the following which also exists..
ReturnValue = Shell("Copy C:\temp\Untitled.png C:\Temp")

Open in new window


Thanks,
Ben
AlanConsultant

Commented:
Okay, but maybe try copying it to a different location such as:

C:\Temp\Test1\

rather than copying it to the same location it is already in.

I'm just trying to get a really clean, unambiguous, test that can't have any other possible issues or unknowns introduced.

This would be perfect (create the Test1 folder manually beforehand):

ReturnValue = Shell("Copy C:\Temp\Untitled.png C:\Temp\Test1\Untitled.png")

Open in new window


If that works, please try this:

ReturnValue = Shell("Copy C:\Users\bfuchs\Downloads\Tables_2017-Nov-05_2053.zip E:\CaspioBackups\Tables_2017-Nov-05_2053.zip")

Open in new window


Thanks,

Alan.
No, followed exactly that & didnt help.
See attached.

Thanks,
Ben
Capture.PNG
AlanConsultant

Commented:
Hi Ben,

Please can you try this form directly in Access VBA:

FileCopy "C:\Temp\Untitled.png", "C:\Temp\Test1\Untitled.png"

Open in new window




 Thanks,

Alan.
Hi Alan,

Not sure if its safe for me to continue using my pc at the moment, as I ran into disaster mode..
See following
https://www.experts-exchange.com/questions/29066852/How-to-recover-deleted-folder-by-code.html

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

I have replied on that question.

Let's put this one on hold until you have resolved the other issue.

Alan.
Hi Alan,

I tried that from debugger window and works.

FileCopy "C:\Temp\Untitled.png", "C:\Temp\Test1\Untitled.png"

Open in new window


So now what should be tried next?

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

Just trying to get my head back into this space :-)

Please can you insert that into your original code, and see if it works like that:

FileCopy "C:\Temp\Untitled.png", "C:\Temp\Test1\Untitled.png"

Open in new window



Thanks,

Alan.
Hi,

This works in middle code too.
Just the E:\CaspioBackups\ location which gives me BAD FILE OR NUMBER error.

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

Have you tried the same form, like this:

FileCopy "C:\Users\bfuchs\Downloads\Tables_2017-Nov-05_2053.zip","E:\CaspioBackups\Tables_2017-Nov-05_2053.zip"

Open in new window


Thanks,

Alan.
OK, This seems to work as well..
So what should I try next..

FYI- The error I'm getting is by following file copy command
    sBackupFolder = "E:\CaspioBackups\"
'    sLastFile = NewestFile(sBackupFolder, "*.zip")
    sDestinationFolder = "C:\Application\"
'    If FileDateTime(sBackupFolder & sLastFile) < Date Then
        sDownloadFolder = DownloadsFolder & "\"
        sLastFile = NewestFile(sDownloadFolder, "*.zip")
        FileCopy sDownloadFolder & sLastFile, sBackupFolder
        UnZip sDownloadFolder & sLastFile, sDestinationFolder, False
    sDestinationFolder = "C:\Application\"
    If Dir(sDestinationFolder & NewestFile(sDestinationFolder, "*.mdb")) <> "" Then
        Kill sDestinationFolder & "Tables.MDB"
        Name sDestinationFolder & NewestFile(sDestinationFolder, "*.mdb") As sDestinationFolder & "Tables.mdb"
    End If

Open in new window


Thanks,
Ben
Consultant
Commented:
Hi Ben,

Did you try this:

FileCopy sDownloadFolder & sLastFile, sBackupFolder & SLastFile

Open in new window



Alan.
OK This seems like working.
Just wondering if file exists, wouldn't I get an error?

Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

See here for how to check if a file exists:

https://www.rondebruin.nl/win/s9/win003.htm

If it exists, then I guess delete it (depending on your scenario) before doing the FileCopy.


Alan.
Hi,

Just to check existence of file, I have it in the code above
 If Dir(sDestinationFolder & NewestFile(sDestinationFolder, "*.mdb")) <> "" Then

Open in new window

Just was wondering why the FileCopy permits me to execute even when file exists?

Thanks,
Ben
Thank you very much Alan!
Hi Alan,

Not sure why but this error (file path not found) started popping up again
Command used as suggested
FileCopy sDownloadFolder & sLastFile, sBackupFolder & SLastFile

Open in new window


and these are the values
filecopy "C:\Users\bfuchs\Downloads\Tables_2017-Nov-15_1108.zip","E:\CaspioBackups\Tables_2017-Nov-15_1108.zip"

Open in new window


Thanks,
Ben
AlanConsultant

Commented:
Hi Ben,

If you put that last full string / command into the immediate window, does it work?

Thanks,

Alan.
No, same error.

Thanks,
Ben
AlanConsultant

Commented:
And if you do it from a command prompt - same machine, same credentials, does it work?

Copy C:\Users\bfuchs\Downloads\Tables_2017-Nov-15_1108.zip E:\CaspioBackups\Tables_2017-Nov-15_1108.zip

Open in new window



Alan.
Hi Alan,

It was my logic error this time, file indeed didn't exists..

I apologize.

Thanks,
Ben
AlanConsultant

Commented:
No problem.  Perhaps add a line to your code to check that the source file exists before doing the filecopy?

Alan.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial