Results 1 to 12 of 12

Thread: [RESOLVED] Adding characters to file name to prevent overwriting?

Threaded View

  1. #2
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Adding characters to file name to prevent overwriting?

    Time for you to look into recursion.

    Code:
    Public Function CheckAndGenerateUniqueCopyName(path As String, Optional seed As Integer = 1) As String
            'Check if the supplied path exists
            If System.IO.File.Exists(path) Then
    
               'The path exists so lets append the seed to the end of it
                path &= "(" & seed & ")"
    
                'Increment the seed value for the next iteration
                seed += 1
                
                'Call the same method with the new parameters until a result is found
                path = CheckAndGenerateUniqueCopyName(path, seed, False)
            End If
    
            Return path
        End Function
    The above code has not been checked, but that is, in general, what you want to do.
    Last edited by wakawaka; Jan 16th, 2013 at 05:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width