user3137157
user3137157

Reputation: 3

VB.NET - Rename all the folders (And sub folders) in a selected folder

I tried to practise, search and i didn't find a solution to how to rename all the folders and sub-folders in a desired folder. For example i want to loop trough all the folders and add "_test" to the end, i searched, practiced and i didn't find any great solution so i'm asking to you if you have any snippet of code, or just and idea. I started with creating an array of all the folders within a folder by doing that:

Dim folderArray() As String = IO.Directory.GetDirectories(TextBoxPath.Text, "*", IO.SearchOption.AllDirectories)

For Each folder In folderArray
    Dim infoParent As New IO.DirectoryInfo(folder)
    Dim folderParent = infoParent.Parent.FullName
    Dim folderName = folder.Split(IO.Path.DirectorySeparatorChar).Last()
    FileSystem.Rename(folder, folderParent & "\" & folderName & "_Test")
Next

But that's not working, because i rename the directories, so the array is not valid (folderArray) because it has the old directory path.

If you have a way to do it, i'm opened to suggestions, thanks.

Upvotes: 0

Views: 4712

Answers (4)

j.i.h.
j.i.h.

Reputation: 827

I would try do it recursively to make sure it's done at the bottom-most level first. (might not be 100% correct code, but just to give the general idea)

Sub RenameFolderRecursive(path As String)
    For Each f As String In IO.Directory.GetDirectories(path)
        RenameFolderRecursive(f)
    Next

    IO.Directory.Move(path, path & "_test")
End Sub

See if that works.

Upvotes: 1

David
David

Reputation: 219127

This sounds like a job for recursion. Since you don't know how many folders any given folder will contain, or how many levels deep a folder structure can be, you can't just loop through it. Instead, write a function which solves a discrete piece of the overall problem and recurse that function. My VB is very rusty so this might not be 100% valid (you'll definitely want to debug it a bit), but the idea is something like this:

Function RenameFolderAndSubFolders(ByVal folder as String)
    ' Get the sub-folders of the current folder
    Dim subFolders() As String = IO.Directory.GetDirectories(folder, "*", IO.SearchOption.AllDirectories)

    If subFolders.Length < 1 Then
        'This is a leaf node, rename it and return
        FileSystem.Rename(folder, folder & "_Test")
        Return
    End If

    ' Recurse on all the sub-folders
    For Each subFolder in subFolders
        RenameFolderAndSubFolders(subFolder)

        ' Rename the current folder once the recursion is done
        FileSystem.Rename(subFolder, subFolder & "_Test")
    Next
End Function

The idea here is simple. Given a folder name, get all of the child folders. If there aren't any, rename that folder. If there are, recurse the same function on them. This should find all of the folders and rename them accordingly. To kick off the process, just call the function on the root folder of the directory tree you want renamed.

Upvotes: 1

Jens
Jens

Reputation: 6385

Private Sub RenameAllFolds(d As String)
    Dim subfolds() As String = IO.Directory.GetDirectories(d)
    If subfolds.Count = 0 Then
        IO.Directory.Move(d, d & "_Test")
    Else
        For Each dir As String In subfolds
            RenameAllFolds(dir)
        Next
        IO.Directory.Move(d, d & "_Test")
    End If
End Sub
Sub Main()
    Dim inf As String = "C:\renametest"
    Dim folds() As String = IO.Directory.GetDirectories(inf)
    For Each f As String In folds
        RenameAllFolds(f)
    Next
    Console.ReadKey()
End Sub

Upvotes: 0

asdawrqtgf
asdawrqtgf

Reputation: 640

Sub RenameAll(ByVal sDir As String)
    Try
        For Each d As String In Directory.GetDirectories(sDir)
            IO.Directory.Move(d, d & "_test")
            RenameAll(d & "_test")
        Next
    Catch x As System.Exception
    End Try
End Sub

Here is recursive function that might do the job you need.

Upvotes: 0

Related Questions