Search code examples
.netfile-rename

Rename files within a folder chronologically by name


I got a little program that renames files within a folder. At this point it's almost working; the only problem that remains is that I want the program to rename the files alphabetically by name. This is my script now:

If TextBox4.Text = "" Or txtbSti.Text = "" Or ComboBox3.Text = "" Then
    MsgBox("Du må velge målmappe og Oppdatere Ønsket Output!", MsgBoxStyle.OkOnly, "Ooooops!")

Else
    Dim AntallFiler = My.Computer.FileSystem.GetFiles(txtbSti.Text)
    Dim antall As Integer = CStr(AntallFiler.Count)
    Dim dir = txtbSti.Text
    Dim type = TextBox4.Text


    Dim sourcePath As String = dir
    Dim searchPattern As String = "*." & ComboBox3.Text
    Dim i As Integer = 1
    For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
        File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, type & i & "." & ComboBox3.Text))
        i += 1
    Next


End If

Solution

  • "chronologically by the name" doesn't make sense ... I'm assuming you mean sorted by name.

    If so, try this:

            Dim list as New List(of string)
            list.AddRange(Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories))
            list.Sort();
    

    Then enumerate the list.

            For Each fileName As String In List
            Next