How to duplicate an existing file (QTP, VBScript)
Parent page: Service Functions – System (QTP, VBScript)
How to duplicate an existing file
(If you want to access the file but it’s locked)
Description
sFileName – name of the file to duplicate
sNewName – name of created file (generated automatically), pass-back argument.
Uses UniqueFilename function to generate a name for the duplicate file.
Public Function DuplicateFile(ByVal sFileName, ByRef sNewName) Dim boolRC Dim FSO, objFile Dim sName, sPath Set FSO = CreateObject("Scripting.FileSystemObject") 'Verify source file exists boolRC = FSO.FileExists(sFileName) If Not boolRC Then Set FSO = Nothing DuplicateFile = FALSE Exit Function End If 'Separate filename and filepath Set objFile = FSO.GetFile(sFileName) sName = objFile.Name sPath = objFile.Path sPath = Left(sPath, Len(sPath)-Len(sName)) Set objFile = Nothing 'Generate unique name for the dup file sNewName = sPath & "\" _ _ & UniqueFilename(sName, sPath, AssociateParameters("prefix = dup, index = 100")) 'Copy file FSO.CopyFile sFileName, sNewName Set FSO = Nothing DuplicateFile = TRUE End Function