VBA to add number to file if already exists

NateD1

New Member
Joined
Apr 1, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I have the below code which imports a file to an access DB then adds the date to the file and moves the imported file to an archive folder.
I would like to add additional code that recognises if the file being moved already exists with the same date and to save a version 2. so essentially add a number to the end of the file name & date when moving if a duplicate. could someone suggest a code that does this? thanks!

VBA Code:
Function Import()

On Error GoTo ImportTaskDataFiles_Err
Dim objFS As Object, objFolder As Object
Dim objFiles As Object, objF1 As Object
Dim strFolderPath As String

strFolderPath = "\filelocation\CSV\"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strFolderPath)
Set objFiles = objFolder.files
  
'Import Txt Files
For Each objF1 In objFiles
If Right(objF1.Name, 3) = "txt" Then

'Add todays date and move file to Archive
    Name strFolderPath & objF1.Name As "\\filelocation\Archive\" & Format(Now, "YYYY-MM-DD ") & objF1.Name
'can the additional code recognises this file already exists with that date and add version control to it?
End If
Next

Set objF1 = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFS = Nothing

MsgBox ("Done")
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Perhaps the below will help with numbering the duplicated files: 1, 2, 3, etc...

VBA Code:
    Dim objFS As Object, objFolder As Object
    Dim objFiles As Object, objF1 As Object
    Dim strFolderPath As String
    Dim strArchivePath As String
    Dim z As Long
    
    strFolderPath = "\filelocation\CSV\"
    strArchivePath = "\\filelocation\Archive\"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    Set objFiles = objFolder.Files
    z = 2
      
    'Import Txt Files
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            'Add todays date and move file to Archive
            If Not objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - 1") Then
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - 1"
            Else
                Do While objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - " & z)
                    z = z + 1
                Loop
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - " & z
            End If
        End If
    Next
    
    Set objF1 = Nothing
    Set objFiles = Nothing
    Set objFolder = Nothing
    Set objFS = Nothing

    MsgBox ("Done")
 
Upvote 0
with this example, i check that file path already exist and file last modified is today then change file path:
VBA Code:
If objFS.FileExists("your file path" & "File extension") And objFS.GetFile("your file path" & "File extension").DateLastModified = Date Then
    For i = 1 To 99
        filepath = "your file path" & " (" & i & ")" & "File extension"
        If Not objFS.FileExists(filepath) And Not objFS.GetFile(filepath).DateLastModified = Date Then
            'rename file
            Exit For
        End If
    Next i
End If
 
Upvote 0
Update:
I missed resetting z back to 2 after moving the file:

VBA Code:
    Dim objFS As Object, objFolder As Object
    Dim objFiles As Object, objF1 As Object
    Dim strFolderPath As String
    Dim strArchivePath As String
    Dim z As Long
    
    strFolderPath = "\filelocation\CSV\"
    strArchivePath = "\\filelocation\Archive\"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    Set objFiles = objFolder.Files
    z = 2
      
    'Import Txt Files
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            'Add todays date and move file to Archive
            If Not objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - 1") Then
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - 1"
            Else
                Do While objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - " & z)
                    z = z + 1
                Loop
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & objF1.Name & " - " & z
                z = 2
            End If
        End If
    Next
    
    Set objF1 = Nothing
    Set objFiles = Nothing
    Set objFolder = Nothing
    Set objFS = Nothing

    MsgBox ("Done")
 
Upvote 0
@Georgiboy amazing thankyou! is there a way to reorder the wording when its moved as it appears like this - 2023-10-23 Daily.txt - 1 and the file doesn't save as a text file due to the number being after .txt
can it be 2023-10-23 Daily -1.txt or 2023-10-23 -1 Daily.txt instead?
 
Upvote 0
You could edit the middle part to:
VBA Code:
    'Import Txt Files
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            'Add todays date and move file to Archive
            If Not objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & " - 1 " & objF1.Name) Then
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & " - 1 " & objF1.Name
            Else
                Do While objFS.fileexists(strArchivePath & Format(Now, "YYYY-MM-DD ") & " - " & z & " " & objF1.Name)
                    z = z + 1
                Loop
                Name strFolderPath & objF1.Name As strArchivePath & Format(Now, "YYYY-MM-DD ") & " - " & z & " " & objF1.Name
                z = 2
            End If
        End If
    Next
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top