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

copying files without overwrite

Status
Not open for further replies.

collierd

MIS
Dec 19, 2001
509
DE
Hello

I am struggling to find how to copy files without overwriting them

Basically, I have tried XCOPY from the command prompt but this seems to just copy regardless

I was going to put this in a batch script for automation

either,

a. Is there another way using XCOPY (or indeed any other command)

b. Can this be automated via vbscript - I know how to automate a script but don;t know the copy and 'not' overwrite command

Thanks

Damian.
 
Hello collierd,

This is the copyfile method. The last parameter specifies the behaviour of overwriting.
documentation said:
CopyFile Method
Copies one or more files from one location to another.

object.CopyFile ( source, destination[, overwrite] )
Arguments
object
Required. The object is always the name of a FileSystemObject.
source
Required. Character string file specification, which can include wildcard characters, for one or more files to be copied.
destination
Required. Character string destination where the file or files from source are to be copied. Wildcard characters are not allowed.
overwrite
Optional. Boolean value that indicates if existing files are to be overwritten. If true, files are overwritten; if false, they are not. The default is true. Note that CopyFile will fail if destination has the read-only attribute set, regardless of the value of overwrite.
Remarks
Wildcard characters can only be used in the last path component of the source argument. For example, you can use:

[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\letters\\*.doc", "c:\\tempfolder\\")
[VBScript]
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"
But you cannot use:

[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\*\\R1???97.xls", "c:\\tempfolder")
[VBScript]
FileSystemObject.CopyFile "c:\mydocuments\*\R1???97.xls", "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 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.
An error also occurs if a source using wildcard characters doesn't match any files. The CopyFile method stops on the first error it encounters. No attempt is made to roll back or undo any changes made before an error occurs.
regards - tsuji
 
Does this imply that an error will occur is the source file already exists in the destination location?

Quote:


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.


I really need for it to ignore it if it already exists

Damian.
 
collierd,

It means that. That means an error handling is encouraged if you want to have good control over the script.
Code:
'other codes
on error resume next
fso.copyfile sourcespec, targetspec, false
if err.number<>0 then
    wscript.echo targetspec & " exists. Copy failed."
    err.clear
else
    wscript.echo targetspec & " established."
end if
on error goto 0
'other codes
- tsuji
 
ok, i'll have a go at that then

Thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top