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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemObject CopyFile

Status
Not open for further replies.

KenWK

Programmer
May 25, 2001
76
US
Working in VB 6 I'm using FilesSystemObject's CopyFolder. The documentation seems to indicate that if there are files of the same name in the destination folder and overwrite is set to false an error occurs. If the destination folder does not exist it works fine. But it seems that if the folder already exists, whether there are files in it or not, a "File already exists" error occurs. I've also tried using a wild card ("\*") at the end of the origin string (as shown in the documentation) to copy the files from the origin to an existing destination folder but am getting a "Path not found" error, presumably from the origin path.

Is there something I'm missing?

Many thanks,
Ken
 

Would it be the case that there is a Read-only file already existinf in the destination folder and you try to overwrite it?
 
No, even if the folder is completely empty I seem to get the error, whether on a local or network drive.

Ken
 
Is there something I'm missing?
Do you mean you don't like the features? or you are not convinced what described in the documentation? If it is the latter, you're not missing anything, it seems. If you don't like the features, I would just say it behaviour exactly as copy and paste in the explorer.
 
tsuji: It does not seem to work as the documentation suggests (maybe I'm misunderstanding the documentation). That is, if the destination folder exists it copies the files into the destination folder unless there are files of the same name in the destination folder.

JerryK: Here is the code:

Function CopyAFolder(spath, dpath, owrite, Reslt) As Boolean
On Error GoTo ERRCTRL
Set fso = CreateObject("scripting.filesystemobject")
fso.CopyFolder spath, dpath, owrite
CopyAFolder = True
ERRCTRL:
If Err <> 0 Then
CopyAFolder = False
Reslt = "Error " & Err.Number & ": " & Err.Description
End If
Set fso = Nothing
End Function



If Not CopyAFolder
("F:\TNTSTUFF\StdStuff\InDesign\junktestfolder", "v:\001234", False, Reslt$) Then
MsgBox Reslt$
End If
End


Thanks again
 
The default value of overwrite is indeed true. First thing to look at is the value of your owrite variable. If you haven't set it to anything, it defaults to false. If you don't want to sometimes overwrite and sometimes not, simply remove this argument.

Second thing to look at is the type of file you want to overwrite. If it's readonly, copyfile will always fail, as the doc mentions.

By the way, this is a more standard setup for error handled code:
Code:
Function CopyAFolder(spath, dpath, owrite, Reslt) As Boolean
    On Error GoTo ERRCTRL
    Set fso = CreateObject("scripting.filesystemobject")
    fso.CopyFolder spath, dpath, owrite
    CopyAFolder = True
    Exit Function
ERRCTRL:
    CopyAFolder = False
    Reslt = "Error " & Err.Number & ": " & Err.Description
End Function
I believe you did it the other way so you wouldn't have to set fso to nothing in two different places. This line isn't necessary.

HTH

Bob
 
>If the destination folder does not exist it works fine
Nope, it's doing something rather different from what you think here (or at least it should be) - because it thinks the destination is a file rather than a folder

>if the folder already exists, whether there are files in it or not, a "File already exists" error
Again, it thinks the destination is a file, and sees that it already exists, so you get the expected error

>to copy the files from the origin to an existing destination folder but am getting a "Path not found" error, presumably from the origin path
Trickier to explain ... I'm going to guess that the destination folder that you are trying to copy to is actually one that you felt had been successfully created during the "If the destination folder does not exist it works fine" phase. As we have established, this is actually a file that gets created, not a folder. So the 'Path Not Found' error message actually refers to the destination, not the source.

And it is all begins with whether you use a trailing \ in the destination (which your example shows that you don't)

From the docs:

If source contains wildcard characters or destination ends with a path separator (\), it is assumed that destination is an existing folder in which to copy matching files. Otherwise, destination is assumed to be the name of a file to create. In either case, three things can happen when an individual file is copied.

If destination does not exist, source gets copied. This is the usual case.

If destination is an existing file, an error occurs if overwrite is false. Otherwise, an attempt is made to copy source over the existing file.

If destination is a directory, an error occurs.

 
Well, the documentation you list appears to me to be the CopyFile command, not the CopyFolder command. The copy folder reads as below. A folder is definately getting created (NOT a file) when the destination DOES NOT already exist. From this documentation if the folder DOES exist I should be able to use CopyFolder "c:\foldername\*", "c:\foldername\" with overwrite set to false and have everything from the source copied into the destination without overwriting, if there is a file of the same name already in the destination an error will occur. Again, I can't seem to get the wildcard chars to work in the source string as they show in the documentation.

Oy!

CopyFolder documentation:

Wildcard characters can only be used in the last path component of the source argument. For example, you can use:

FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"

But you can't use:

FileSystemObject.CopyFolder "c:\mydocuments\*\*", "c:\tempfolder\"

If source contains wildcard characters or destination ends with a path separator (\), it is assumed that destination is an existing folder in which to copy matching folders and subfolders. Otherwise, destination is assumed to be the name of a folder to create. In either case, four things can happen when an individual folder is copied.

If destination does not exist, the source folder and all its contents gets copied. This is the usual case.

If destination is an existing file, an error occurs.

If destination is a directory, an attempt is made to copy the folder and all its contents. If a file contained in source already exists in destination, an error occurs if overwrite is False. Otherwise, it will attempt to copy the file over the existing file.

If destination is a read-only directory, an error occurs if an attempt is made to copy an existing read-only file into that directory and overwrite is False.
An error also occurs if a source using wildcard characters doesn't match any folders.

 
No apologies necessary. I guess I'll just assume it doesn't work exactally as the documentation says.

If anyone has any further ideas, I'm open to them.

Many thanks for all the work!

Ken
 
>It does not seem to work as the documentation suggests (maybe I'm misunderstanding the documentation). That is, if the destination folder exists it copies the files into the destination folder unless there are files of the same name in the destination folder.

I hope that is not exact quotation from the documentation.

Leaving the possibility of wildcard for multiple folder copying, suppose the srcfolder and tgtfolder like this.
[tt]
srcfolder="d:\abc\def" 'a folder existing
tgtfolder="d:\pqr\stu" 'without trailing backslash
[/tt]
The documentation suggests

[a] the If condition being met, error will be triggered (this is obvious).

[tt] if fso.folderexists(tgtfolder) then
fso.copyfolder srcfolder,tgtfolder,false
end if[/tt]

If condition being met, error will be triggered as well, ie, if there is a "file" of the name of tgtfolder, here d:\pqr\stu, a perfectly valid file name, disregard overwrite being true or false: meaning you cannot overwrite a "file" from copyfolder operation even the 3rd parameter is set to true, not to say set to false.

[tt] if fso.[blue]file[/blue]exists(tgtfolder) then
'any of these will be error out
fso.copyfolder srcfolder,tgtfolder,false
'fso.copyfolder srcfolder,tgtfolder 'implicitly or explicitly overwrite true[/tt]

[b.1] (This note is targetted to the quote in the thread.) If the tgtfolder exists already, the name of the "files" [red]within[/red] that folder does not matter. They will be clear out in any case. The obvious way to verify it is to run twice the copyfolder.
[tt]
fso.copyfolder srcfolder,tgtfolder,true
fso.copyfolder srcfolder,tgtfolder,true 'no error
[/tt]

[blue]attachment [excerpt from documentation][/blue]
[tt]
If destination does not exist, the source folder and all its contents gets copied. This is the usual case.

If destination is an existing file, an error occurs.

If destination is a directory, an attempt is made to copy the folder and all its contents. If a file contained in source already exists in destination, an error occurs if overwrite is false. Otherwise, it will attempt to copy the file over the existing file.

If destination is a read-only directory, an error occurs if an attempt is made to copy an existing read-only file into that directory and overwrite is false.

An error also occurs if a source using wildcard characters doesn't match any folders.
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top