Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

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

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
Avatar of arnold
arnold
Flag of United States of America image

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

...
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.
Could also be that file is locked, path too long errors. That error can mean a bunch of things
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
Avatar of bfuchs

ASKER

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
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.
Avatar of bfuchs

ASKER

@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
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.
Avatar of bfuchs

ASKER

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
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.
Avatar of bfuchs

ASKER

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
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.
Avatar of bfuchs

ASKER

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

Thanks,
Ben
Untitled.png
Capture.PNG
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.
Avatar of bfuchs

ASKER

Hi

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

Open in new window


Thanks,
Ben
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.
Avatar of bfuchs

ASKER

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

Thanks,
Ben
Capture.PNG
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.
Avatar of bfuchs

ASKER

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
Hi Ben,

I have replied on that question.

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

Alan.
Avatar of bfuchs

ASKER

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
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.
Avatar of bfuchs

ASKER

Hi,

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

Thanks,
Ben
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.
Avatar of bfuchs

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Alan
Alan
Flag of New Zealand 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
Avatar of bfuchs

ASKER

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

Thanks,
Ben
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.
Avatar of bfuchs

ASKER

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
Avatar of bfuchs

ASKER

Thank you very much Alan!
Avatar of bfuchs

ASKER

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
Hi Ben,

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

Thanks,

Alan.
Avatar of bfuchs

ASKER

No, same error.

Thanks,
Ben
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.
Avatar of bfuchs

ASKER

Hi Alan,

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

I apologize.

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

Alan.