Copy File Creating Name 'Copy (n) of' - February 2004

When making a backup copy of a file it is sometimes useful to create a new name using a format similar to 'Copy (5) of MyFile.txt'. I found that API SHFileOperation is useful for copying, moving and deleting files. So in this tip I show how to use SHFileOperation to copy a file but I've added a class module to perform the copy function and if needed prefix the file name with 'Copy Of' to avoid conflicts.

February 8, 2005 - Updated - The class module is updated to always create the next highest number in the 'Copy (x) of' file name. The original version used the next available number. The new version examines all the file names in the 'to' folder then assigns the highest number plus one. So if the user has deleted an intermediate number the highest number plus one will be used. For example assume the following list of files:

Copy (1) of MySampleFile.txt
Copy (2) of MySampleFile.txt
Copy (8) of MySampleFile.txt

The original version would have next created 'Copy (3) of MySampleFile.txt'. The new version will next create 'Copy (9) of MySampleFile.txt'.

February 16, 2006 - Updated - The class module not accepts an alternate prefix, for example: Backup. And the number of existing copies of the file are returned [.iExistingCopies]. In the above sample box this number would be 3. See below for details.

Demo Program

I wrote a VB6 demo program to illustrate the use of the class. The demo:

Copy, Move or Delete a File
Copy, Move or Delete a File

From - The fully qualified file to be copied. Use the 'browse' button to locate a file. Default is 'app.path\MySampleFile.txt'.

Prefix - The prefix to be appended to the file name. Default is 'Copy'. Could be 'Backup' or any word or words desired.

Options Copy, Move, Delete - Select the action to be performed.

Do ... File - Click this button to perform the selected action. The command caption changes based upon the option selected.

To - The resulting file name. No entry allowed in the text field.

Delete File - Delete the copied file.

Status - The number of copies of the file in the folder [.iExistingCopies] is reported here.

Sample Calling Code

Private Function DoCopy() As Boolean
  Dim o As New cCopyOfName
  With o 'use the class module to copy the file
    .Prefix = "Backup"
    If .CopyFile(Me.Text1(0).Text, , Me.hWnd) Then
      Me.Text1(1).Text = .sToFileName
      DoCopy = True
    End If
  End With
End Function

Class cCopyOfName Methods and Properties

cCopyOfName Methods and Properties

Methods/Properties Operands Sample
.CopyFile [sFromFileName] [,sToFileName] [,hWnd], [sPrefix] If .CopyFile "MyFile.txt", , Me.hWnd) Then
 .sToFileName Fully Qualified File Name to be Copied "c:\MyFolder\MyFile.txt"
 .sToFileName Destination File Name, not normally used.  
 .hWnd Handle to the current form. Me.hWnd
 .sPrefix Added to the beginning of the file name, default is 'Copy'. .sPrefix = "Backup"
 .iExistingCopies Returned from the CopyFile operation. The actual number of copies not the copy number. Me.txtCopies.Text = .iExistingCopies
.FileExists sFullyQualifiedFileName
Uses API GetFileAttributes which avoids opening the file.
If FileExists(c:'MyFolder\MyFile.txt) Then

The Demo Program Uses These Modules

Module - Click for More Info Description Used in the Demo To
cCopyOfName.cls Copy and Create The subject of this tip.
Include in your application.
modSHFileOperation.bas Use this API to copy, move, delete. Include in your application.
clsSHAutoComplete.cls Auto complete file names and URLs Complete the 'from' file name.
cDialog.cls Browse for file by Francesco Balena Browse for file.
cFilenameParse.cls Parse a fully qualified file name Change the sample caption.
modPath.bas Parse a fully qualified file name Parse the 'from' and 'to' file names.
modAddBackSlash.bas Add a Backslash if Needed Add a backslash to App.Path, if needed.
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is
selected from the demo.
modStatusV2.bas Provide status information Report existing file information.
modInfoAbout.bas DoInfo, DoAbout, DoTip Link to this page and provide helpful information

Download VB Code

Click here to download the VB 6 source code [28k].

Previous Tip   Tip Index   Next Tip


Updated 2006/02/18