Search code examples
vb.netfilerenamecycle

Visual Studio rename files on a directory with cycle for and substitute it with text from a textbox


I need help with the cycle for, when program start should rename files on selected directory but it doesn't.

This is the code:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        Label4.Text = FolderBrowserDialog1.SelectedPath
        Dim counter = FolderBrowserDialog1.SelectedPath
        Label5.Text = counter.Count
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim basedir As String = FolderBrowserDialog1.SelectedPath
        For counter As Integer = 0 To Int(Label5.Text)
            My.Computer.FileSystem.RenameFile(basedir, TextBox1.Text + "x")
        Next
    End Sub
End Class

Solution

  • First of all, FolderBrowserDialog1.SelectedPath returns a normal string. Calling SelectedPath.Count will not give you how many files are in the directory, but how many character the string path consists of.

    Secondly, calling RenameFile(basedir, ...) won't do anything since basedir points to a single directory - not the files in the directory.

    In order to get a proper file count, AND iterate through the file names you'd have to use something like Directory.GetFiles(basedir).

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        Label4.Text = FolderBrowserDialog1.SelectedPath
        Label5.Text = Directory.GetFiles(FolderBrowserDialog1.SelectedPath).Count
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim basedir As String = FolderBrowserDialog1.SelectedPath
        Dim Files As String() = Directory.GetFiles(basedir) 'Declare an array that holds all file paths.
        For counter As Integer = 0 To Files.Length - 1 'Minus one is important here, so that you won't get a "IndexOutOfRangeException".
            My.Computer.FileSystem.RenameFile(Files(counter), TextBox1.Text & counter)
        Next
    End Sub
    

    Files(counter) represents the file that it's currently at. If counter = 0 then Files(counter) would be the first file path in the array, and so on.

    One thing I don't understand is why you give the new files the name of whatever you put in TextBox1 and an x. You are currently giving every file in that directory the exact same name (which is impossible). Could you tell me what you really are trying to rename them to?

    In the mean time I replaced the x with counter. That will rename them to <whatever is in TextBox1>0, <whatever is in TextBox1>1, and so on.


    One last thing, as you might have noticed I put the ampersand (&) instead of the plus sign (+) in the second parameter of RenameFile. Using the ampersand is the recommended way of concatenating strings, as the plus sign could cause exceptions in certain cases.


    EDIT:

    To keep the extension of the file you just have to extract it, and then add it to the new name:

    For counter As Integer = 0 To Files.Length - 1
        Dim FileExtension As String = Path.GetExtension(Files(counter)) 'Keeping the extension.
        My.Computer.FileSystem.RenameFile(Files(counter), TextBox1.Text & counter & FileExtension)
    Next