taji01
taji01

Reputation: 2615

Remove part of folder name in bulk that’s inside a specific directory

In my "C:\image\Test_Directory\" i have various folders named {hello1, hello2, hello3, hello4} etc... I wanted to remove the word hello in my folders under my "C:\image\Test_Directory\" so the folders would read {1, 2, 3, 4} etc... I searched online on how to do this but i had no luck.

Example Issue:

enter image description here

GOAL:

enter image description here

Upvotes: 1

Views: 230

Answers (2)

JPil
JPil

Reputation: 301

Use RenameDirectory() function Try this :

 Dim dir As DirectoryInfo = New DirectoryInfo("C:\image\Test_Directory")
    Dim folders As DirectoryInfo() = dir.GetDirectories()
    For Each folder As DirectoryInfo In folders
        My.Computer.FileSystem.RenameDirectory(folder.FullName, folder.Name.Replace("Hello", ""))
    Next

Upvotes: 1

itsme86
itsme86

Reputation: 19496

You'd use Directory.Move() to rename the folders:

foreach (var dirName in Directory.GetDirectories(@"C:\image\Test_Directory"))
{
    string newName = dirName.Replace("Hello", "");
    Directory.Move(dirName, newName);
}

Upvotes: 2

Related Questions