renaming file names-VBForums
Results 1 to 5 of 5

Thread: renaming file names

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Question renaming file names

    hi can someone help me with the code for renaming existing files? thanks alot!

    this is what ive wrote.. and it fails to rename existing files
    Code:
                 For Each file As String In listbox1.Items
                        IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
                        While IO.File.Exists(fbCopyto.SelectedPath & file)
                            file=  file & x & ".wav"
                            x += 1
    
    
                        End While
                    Next

  2. #2
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: renaming file names

    Code:
                 For Each file As String In listbox1.Items
                        IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
                        While IO.File.Exists(fbCopyto.SelectedPath & file)
                            IO.File.Rename(file, "new name.wav")
                            x += 1
                        End While
                    Next
    I'm not quite sure why you have a while statement in there for, but I wasn't sure of your context so I left it

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,598

    Re: renaming file names

    Presumably you are trying to copy a file and, if a file with that name exists, you change the name numerically. E.g. if Test.txt exists then you try Test1.txt, and if that exists you try Test2.txt, etc. Here's some code that will do that:
    vb.net Code:
    1. Dim destinationFolderPath As String = fbCopyTo.SelectedPath
    2.  
    3. For Each sourceFilePath As String In Me.ListBox1.Items
    4.     Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath))
    5.  
    6.     If IO.File.Exists(destinationFilePath) Then
    7.         Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath)
    8.         Dim fileNumber As Integer = 1
    9.         Dim extension As String = IO.Path.GetExtension(sourceFilePath)
    10.  
    11.         destinationFolderPath = IO.Path.Combine(destinationFolderPath, _
    12.                                                 String.Format("{0}{1}.{2}", _
    13.                                                               fileName, _
    14.                                                               fileNumber, _
    15.                                                               extension))
    16.  
    17.         While IO.File.Exists(destinationFolderPath)
    18.             fileNumber += 1
    19.             destinationFolderPath = IO.Path.Combine(destinationFolderPath, _
    20.                                                     String.Format("{0}{1}.{2}", _
    21.                                                                   fileName, _
    22.                                                                   fileNumber, _
    23.                                                                   extension))
    24.         End While
    25.     End If
    26.  
    27.     IO.File.Copy(sourceFilePath, destinationFilePath)
    28. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: renaming file names

    hi jmcilhinney, thanks for the reply!

    ive tried your code, unfortunately, i still cannot rename the file that has existing copy of it.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,598

    Re: renaming file names

    There was an error in my code. I used 'destinationFolderPath' in a few places where I should have used 'destinationFilePath'. Here's the corrected version:
    Code:
    Dim destinationFolderPath As String = fbCopyTo.SelectedPath
    
    For Each sourceFilePath As String In Me.ListBox1.Items
        Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath))
    
        If IO.File.Exists(destinationFilePath) Then
            Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath)
            Dim fileNumber As Integer = 1
            Dim extension As String = IO.Path.GetExtension(sourceFilePath)
    
            destinationFilePath = IO.Path.Combine(destinationFolderPath, _
                                                  String.Format("{0}{1}.{2}", _
                                                                fileName, _
                                                                fileNumber, _
                                                                extension))
    
            While IO.File.Exists(destinationFilePath)
                fileNumber += 1
                destinationFilePath = IO.Path.Combine(destinationFolderPath, _
                                                      String.Format("{0}{1}.{2}", _
                                                                    fileName, _
                                                                    fileNumber, _
                                                                    extension))
            End While
        End If
    
        IO.File.Copy(sourceFilePath, destinationFilePath)
    Next
    You probably could have worked that out for yourself with a bit of an examination of the code and some debugging.

    Here's a slightly more succinct version:
    vb.net Code:
    1. Dim destinationFolderPath As String = fbCopyTo.SelectedPath
    2.  
    3. For Each sourceFilePath As String In Me.ListBox1.Items
    4.     Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath))
    5.  
    6.     If IO.File.Exists(destinationFilePath) Then
    7.         Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath)
    8.         Dim fileNumber As Integer = 0
    9.         Dim extension As String = IO.Path.GetExtension(sourceFilePath)
    10.  
    11.         Do
    12.             fileNumber += 1
    13.             destinationFilePath = IO.Path.Combine(destinationFolderPath, _
    14.                                                   String.Format("{0}{1}.{2}", _
    15.                                                                 fileName, _
    16.                                                                 fileNumber, _
    17.                                                                 extension))
    18.         Loop While IO.File.Exists(destinationFilePath)
    19.     End If
    20.  
    21.     IO.File.Copy(sourceFilePath, destinationFilePath)
    22. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.