ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   object.CopyFile() has an error? (https://www.excelbanter.com/excel-programming/451238-object-copyfile-has-error.html)

Robert Crandal[_3_]

object.CopyFile() has an error?
 
Sub Test()

Dim fo As Object

Set fo = CreateObject("Scripting.FileSystemObject")

fo.CopyFile "C:\file1.txt", "C:\data\file1.txt", False

End Sub


Before you run the above code, create an empty directory
of "C:\data". Oh course, "file1.txt" must exist on C: as well.

If you run the above code once, file1.txt will successfully
be copied into C:\data. However, run the code for a second
time and you get a run-time error 58 message that says:
"File already exists".

Why does this error message appear? I've set the overwrite
option to False, meaning do NOT overwrite.

If I set the overwrite flag to True, the file inside C:\data
never gets overwritten. No matter how many times I try.
(Neither files are set to Read Only)

So, I'm getting an error message when I use the False
option. And, I'm unable to overwrite the file if I use the
True flag.

What is going on here????





Claus Busch

object.CopyFile() has an error?
 
Hi Robert,

Am Sat, 26 Dec 2015 03:16:34 -0700 schrieb Robert Crandal:

If you run the above code once, file1.txt will successfully
be copied into C:\data. However, run the code for a second
time and you get a run-time error 58 message that says:
"File already exists".
Why does this error message appear? I've set the overwrite
option to False, meaning do NOT overwrite.


VBA help says that you get this error if the file in the target exists
and overwrite = false
So you have to check if the file exists before running the code.

If I set the overwrite flag to True, the file inside C:\data
never gets overwritten. No matter how many times I try.
(Neither files are set to Read Only)


I had no problems to overwrite the file. True is default. Don't need to
write it:
fo.CopyFile "C:\file1.txt", "C:\Ordner1\file1.txt"
Do you have all the rights for "C:\" ?


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional

GS[_6_]

object.CopyFile() has an error?
 
Sub Test()

Dim fo As Object

Set fo = CreateObject("Scripting.FileSystemObject")

fo.CopyFile "C:\file1.txt", "C:\data\file1.txt", False

End Sub


Before you run the above code, create an empty directory
of "C:\data". Oh course, "file1.txt" must exist on C: as well.

If you run the above code once, file1.txt will successfully
be copied into C:\data. However, run the code for a second
time and you get a run-time error 58 message that says:
"File already exists".

Why does this error message appear? I've set the overwrite
option to False, meaning do NOT overwrite.

If I set the overwrite flag to True, the file inside C:\data
never gets overwritten. No matter how many times I try.
(Neither files are set to Read Only)

So, I'm getting an error message when I use the False
option. And, I'm unable to overwrite the file if I use the
True flag.

What is going on here????


Since you're using FSO.CopyFile instead of VBA's CopyFile, the
Script56.chm states...

"Note that CopyFile will fail if *destination* has the read-only
attribute set, regardless of the value of overwrite."

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Robert Crandal[_3_]

object.CopyFile() has an error?
 

"GS" wrote:

Since you're using FSO.CopyFile instead of VBA's CopyFile, the
Script56.chm states...

"Note that CopyFile will fail if *destination* has the read-only
attribute set, regardless of the value of overwrite."


I found that the C:\data folder has the Read Only attribute set.
I tried removing it but it won't go away. If the folder is Read Only,
why does the code write the file into the folder once, but on the
second time it fails?

BTW, I'm running this code on a personal laptop.




GS[_6_]

object.CopyFile() has an error?
 
"GS" wrote:

Since you're using FSO.CopyFile instead of VBA's CopyFile, the
Script56.chm states...

"Note that CopyFile will fail if *destination* has the read-only
attribute set, regardless of the value of overwrite."


I found that the C:\data folder has the Read Only attribute set.
I tried removing it but it won't go away. If the folder is Read
Only,
why does the code write the file into the folder once, but on the
second time it fails?

BTW, I'm running this code on a personal laptop.


Changing the attribute via the Properties dialog does nothing if
Permissions do not allow 'write'!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Claus Busch

object.CopyFile() has an error?
 
Hi Robert,

Am Sat, 26 Dec 2015 13:18:24 -0700 schrieb Robert Crandal:

I found that the C:\data folder has the Read Only attribute set.
I tried removing it but it won't go away. If the folder is Read Only,
why does the code write the file into the folder once, but on the
second time it fails?


if overwrite is false you try to store the same file with the same name
into the same folder twice. That is not allowed.


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional

Robert Crandal[_3_]

object.CopyFile() has an error?
 

"Claus Busch" wrote:

if overwrite is false you try to store the same file with the same name
into the same folder twice. That is not allowed.


I am currently running this code, with the True option:

fo.CopyFile "C:\file1.txt", "C:\data\file1.txt", True

The script56.chm file states the following about the overwrite option:

"If true, files are overwritten"

How can I tell that the file was successfully overwritten? When I
right-click on file1.txt and look at the properties I see no changes
to the Created, Modified, or Accessed status fields. It shows that
the file in the C:\data folder was created 4 hours ago, and was last
accessed and created 1 hour ago.




GS[_6_]

object.CopyFile() has an error?
 
"Claus Busch" wrote:

if overwrite is false you try to store the same file with the same
name
into the same folder twice. That is not allowed.


I am currently running this code, with the True option:

fo.CopyFile "C:\file1.txt", "C:\data\file1.txt", True

The script56.chm file states the following about the overwrite
option:

"If true, files are overwritten"

How can I tell that the file was successfully overwritten? When I
right-click on file1.txt and look at the properties I see no changes
to the Created, Modified, or Accessed status fields. It shows that
the file in the C:\data folder was created 4 hours ago, and was last
accessed and created 1 hour ago.


The key words in the script56.chm are...

"..if destination has the 'read only' attribute set."

...which applies to the folder the file resides in since this is
inherited.

'Read only' on the file itself governs how apps can interact with the
file. (Save not allowed; SaveAs allowed)

'Read only' on the folder means you can't overwrite existing files in
there. Thus, the folder must have the 'Modify' *permission* set for the
current user to replace/overwrite its files.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Robert Crandal[_3_]

object.CopyFile() has an error?
 
"GS" wrote:

The key words in the script56.chm are...

"..if destination has the 'read only' attribute set."

..which applies to the folder the file resides in since this is inherited.

'Read only' on the file itself governs how apps can interact with the
file. (Save not allowed; SaveAs allowed)

'Read only' on the folder means you can't overwrite existing files in
there. Thus, the folder must have the 'Modify' *permission* set for the
current user to replace/overwrite its files.


I confirmed that yes, the code DOES overwrite the contents of
C:\data\file1.txt.

What was confusing to me was the Windows properties dialog.
None of the status fields, such as Created, Modified, or Accessed,
had changed. Those fields reflect the status of C:\file1.txt, not
C:\data\file1.txt!

Anyways, the code seems to be working to my liking. I was
simply just using wrong methods to detect if the file was
overwritten or not.





All times are GMT +1. The time now is 08:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com