Rename Files and Folders to Lowercase


Imports System.IO

Module Module1
    Sub Main2()
        Dim rootPath As String = "C:\Path\To\Your\Folder"
        ConvertToLowercase(rootPath)
        Console.WriteLine("Renaming complete.")
        Console.ReadLine()
    End Sub

    Sub ConvertToLowercase(path As String)
        ' Process subdirectories first (recursion)
        For Each dir As String In Directory.GetDirectories(path)
            ConvertToLowercase(dir)
        Next

        ' Rename directories
        For Each dir As String In Directory.GetDirectories(path)
            Dim dirInfo As New DirectoryInfo(dir)
            Dim finalName As String = dirInfo.Name.ToLower()

            If dirInfo.Name <> finalName Then
                Dim parentDir As String = dirInfo.Parent.FullName
                Dim tempName As String = System.IO.Path.Combine(parentDir, Guid.NewGuid().ToString()) ' Temporary name to force rename
                Dim tempPath As String = tempName

                Try
                    ' Rename to temporary name
                    Directory.Move(dir, tempPath)
                    ' Rename to final lowercase name
                    Dim finalPath As String = System.IO.Path.Combine(parentDir, finalName)
                    Directory.Move(tempPath, finalPath)
                    Console.WriteLine($"Renamed directory: {dir} -> {finalPath}")
                Catch ex As Exception
                    Console.WriteLine($"Error renaming directory {dir}: {ex.Message}")
                End Try
            End If
        Next

        ' Rename files
        For Each file As String In Directory.GetFiles(path)
            Dim fileInfo As New FileInfo(file)
            Dim finalName As String = fileInfo.Name.ToLower()

            If fileInfo.Name <> finalName Then
                Dim dir As String = fileInfo.DirectoryName
                Dim tempName As String = System.IO.Path.Combine(dir, Guid.NewGuid().ToString() & fileInfo.Extension)
                Dim tempPath As String = tempName

                Try
                    ' Rename to temporary name
                    System.IO.File.Move(file, tempPath)
                    ' Rename to final lowercase name
                    Dim finalPath As String = System.IO.Path.Combine(dir, finalName)
                    System.IO.File.Move(tempPath, finalPath)
                    Console.WriteLine($"Renamed file: {file} -> {finalPath}")
                Catch ex As Exception
                    Console.WriteLine($"Error renaming file {file}: {ex.Message}")
                End Try
            End If
        Next
    End Sub
End Module

Download 'Files Folders toLower.vb':

📥 Download files-folders-tolower.vb