19 November 2012

Increment File names If File already exists in Designation Path

Private Sub FileNameIncrement(SourcePath As String, DesignationPath As String)
    Dim dir As New DirectoryInfo(SourcePath)
    Dim fileName As String = dir.Name
    Dim fileNamewithoutExtension As String = Path.GetFileNameWithoutExtension(DesignationPath & "\" &
fileName)
    Dim fileExtention As String = Path.GetExtension(DesignationPath & "\" & fileName)
    Dim files As String() = Directory.GetFiles(DesignationPath)
    Dim count As Integer = files.Count(Function(file__1)
    Return file__1.Contains(fileName)
End Function)
    Dim newFileName As String = If((count = 0), fileName, [String].Format("{0} ({1})" & fileExtention, fileName,
count + 1))
    If File.Exists(DesignationPath & "\" & fileName) Then
        File.Copy(SourcePath, DesignationPath & "\" & newFileName)
    Else
        File.Copy(SourcePath, DesignationPath & "\" & fileName)
    End If
End Sub

Dim OutputPath As String = "C:\Documents and Settings\Naraayanan\My Documents\Downloads"
FileNameIncrement(textBox1.Text, OutputPath)

No comments: