If file already exist in a designated path, add a index # counter in the file name

Piaba

New Member
Joined
Jan 31, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Good morning everyone,

I'd like to make my code to check if a file with same name is in the folder already, if so, make it to save with a different number index starting with 1. Here is the code that I'm using now:

VBA Code:
Sub TestFile

Dim FilePathandName As String

Set fs = CreateObject("scripting.filesystemobject")

FilePathandName = Application.ActiveWorkbook.FullName

Set file = fs.CreateTextFile(Mid(FilePathandName, 1, InStrRev(FilePathandName, ".") - 1) & ".kml", True, True)

End Sub

Can anybody please help me?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Here's something I came up with. Might not be as efficient as possible, but it's a start. If you have a Test.kml file, it will create a Test1.kml file, etc.

I didn't touch the "Set file" part of the code because I'm not sure what you are doing there. It seems like you are just creating a blank file so far, which might be your intention. If you need more, your code will have to spell it out.

VBA Code:
Sub TestFile()

Dim FilePathandName As String
Dim fs As FileSystemObject

Set fs = CreateObject("scripting.filesystemobject")

FilePathandName = GetFilename(Application.ActiveWorkbook.FullName)

Set file = fs.CreateTextFile(FilePathandName, True, True)

End Sub

Function GetFilename(fName As String) As String
    Dim i As Integer
    Dim sPath As String
    Dim sName As String
    Dim sFile As String
    
    sPath = Mid(fName, 1, InStrRev(fName, "\"))
    sName = Mid(fName, InStrRev(fName, "\") + 1)
    sName = Mid(sName, 1, InStrRev(sName, ".") - 1)
    sFile = Dir(sPath & sName & ".kml")
    Do While sFile <> ""
        i = i + 1
        sFile = Dir(sPath & sName & i & ".kml")
    Loop
    GetFilename = sPath & sName & i & ".kml"
End Function
 
Upvote 0
Solution
@Piaba I think you are missing a loop in there but the following should work once you include the rest of your code:

VBA Code:
Dim i As Long

Sub TestFile()
'
    Dim FSO             As Object
    Dim FilePathandName As String
'
    Set FSO = CreateObject("scripting.filesystemobject")
'
    FilePathandName = GetNextAvailableFileName(Mid(Application.ActiveWorkbook.FullName, 1, InStr(1, Application.ActiveWorkbook.FullName, ".") - 1) & ".klm")
'
    Set file = FSO.CreateTextFile(FilePathandName, True, True)
'
End Sub

Function GetNextAvailableFileName(ByVal strPath As String) As String
'
    Dim FSO         As Object
    Dim strFolder   As String
    Dim strBaseName As String
    Dim strExt      As String
'
    With CreateObject("Scripting.FileSystemObject")
        strFolder = .GetParentFolderName(strPath)
        strBaseName = .GetBaseName(strPath)
        strExt = .GetExtensionName(strPath)
'
        Do While .FileExists(strPath)
            i = i + 1
            strPath = .BuildPath(strFolder, strBaseName & "-" & i & "." & strExt)
        Loop
    End With
'
    GetNextAvailableFileName = strPath
End Function
 
Upvote 0
Good morning,

Tried you guys codes and both worked for the macro that I wanted to create. Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,215,762
Messages
6,126,736
Members
449,334
Latest member
moses007

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