Issue with MoveFile method to overwrite file in Destination in vbscript?

user1587060 picture user1587060 · Jun 12, 2013 · Viewed 65.3k times · Source

I have a vbscript that I have written to move files from a source directory to a destination directory. The way the script works at the moment is that I have a mapping file which is read in (maps id's to folder type). Each file being moved begins with the id and the destination will be based on what the id is mapped to. I read in the mapping file and build up the destination path for each file being moved. This all works as expected, the problem is when I try to move a file that already exists in the destination directory, the files are not moved from the source directory. Essentially I would like it to overwrite a file in the destination directory if it already exists. At the moment, my main command is this:

fso.MoveFile ObjFile.Path, archiveTo & "\" & yearValue & "\" & monthValue & "\" & ObjFile.Name

Is there a way to default this to always overwrite a file in the destionation directory if it already exists?

Answer

Unfortunately, the VBScript MoveFile method works only when the target file does not exist. It can't overwrite such file when exists, just throw error.

So the only option is to use CopyFile (which does have option to overwrite) then DeleteFile:

fso.CopyFile ObjFile.Path, archiveTo & "\" & yearValue & "\" & monthValue & "\" & ObjFile.Name, True
fso.DeleteFile ObjFile.Path