Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Dealing with duplicate folders vb.net?

I am making a program where it makes folders based on an entered name. So if the person makes 2 or more of the same entries I need it to be able to change the folder name so it doesn't overlap. I need a rename system like windows explorer. Where it will add a (1) or (2) to the end. How do I do this. I can't do if (exists) then (exists) = (exists) & (1), because then if someone makes a 3rd entry with the same name, and it tried to rename it to have a (1) it will overlap the second entry.

2 Answers

Relevance
  • 9 years ago
    Favorite Answer

    '

    ' Call this and pass in the folder name you want to use. If it is already in use it will find the next

    ' available (#) entry and return that as the name to use.

    Private Function AvailableName(byref inFolder As String) As String

    If My.Computer.FileSystem. FolderExist(inFolder) = False Then Return inFolder

    Dim myIncrementer As Integer = 1

    Dim outFolder As String = inFolder & "(1)"

    Do Until My.Computer.FileSystem. FolderExist(outFolder) = False

    myIncrementer += 1

    outFolder = inFolder & "(" & myIncrementer & ")"

    Loop

    Return outFolder

    End Function

    Source(s): VB.NET Programmer
  • 9 years ago

    if filename.exists {

    for (x=0;x<=9999;x++)

    {

    if (newname.exists)

    {

    newname = filename.exists + i

    }

    }

    if (newname == filename + 9999)

    {

    print "Filename exists! enter a new name\n"

    }

    }

    This is just psuedo code you need to adapt the function to your needs

Still have questions? Get your answers by asking now.