Add to folder name

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
I have a directory with a bunch of sub folders. I need to add a number and underscore to the main folder and all sub folders.

Example

Subfolder
Atmos_Energy

I need it to say
05_Atmos_Energy

There will be 500 subfolders

How would I have a form that can name all the subfolders at once?

Thanks
 
Those the variables were just to store the directory path and set the prefix, you could exclude them and just use something like "renameFolders("C:\Folder", "01_")".

For the newname issue, use this instead:

VB.NET:
Imports System.IO Public Class Form1     
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       
  Dim rootFolderPath As String = "C:\New Folder1"        
Dim numberPrefix As String = "01_"        
renameFolders(rootFolderPath, numberPrefix)     
End Sub       
Private Sub renameFolders(ByVal strRoot As String, ByVal prefixNumber As String)         
Dim subFolders() As String = Directory.GetDirectories(strRoot)
Dim newname As String = ""         
For Each direct As String In subFolders             
newname = prefixNumber & IO.Path.GetFileName(direct)             
My.Computer.FileSystem.RenameDirectory(direct, newname)         
Next        
newname = prefixNumber & IO.Path.GetFileName(strRoot)         
My.Computer.FileSystem.RenameDirectory(strRoot, prefixNumber & newName)    
 End Sub
End Class
 
Sure, ex:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    renameFolders("C:\Folder1", "01_")  
End Sub
 
Nevermind.... I am an idiot. lol. Forgot the Load Event was controlling the procedure....

However, last but not least -

When the folders are rename the main folder is named with the number twice......
Example;

C:\test\newfolder

(with sub folders)

All the sub folders are perfect, however, the newfolder has 01_01_NewFolder

Any suggestions?

Thanks
 
I guess I am tired today :). I accidentally had the renameFolders function add that two times!

Replace this line in the renameFolders funtion:
My.Computer.FileSystem.RenameDirectory(strRoot, prefixNumber & newName)
with this:
My.Computer.FileSystem.RenameDirectory(strRoot, newName)

Sorry about that.
 
Also, (not saying I did.....), but what if I accidently selected the wrong directory and just renamed 300 or so sub folders as 02_ instead of 03_?

Is there a simple way to delete the 02_?
Also, about the previous post about double naming.......

:)

After this I will be quiet for awhile......

daveofgv
 
Try something like this:

Private Sub removePrefix(ByVal strRoot As String, ByVal strPrefix As String)
    For Each strFolder As String In Directory.GetDirectories(strRoot)
        Dim newName As String = strFolder.Replace(strPrefix, "")
        My.Computer.FileSystem.RenameDirectory(strFolder, newName)
    Next
End Sub
 
VB.NET:
removePrefix("C:\Folder1", "02_")
 
Joshdbr -

I have a couple textbox's that I have attached to a My.Settings. I have successfully finished the add prefix part, however, I am now having a liitle trouble on the removal part.

I have presently:

VB.NET:
   Private Sub removePrefix()
        Dim strRoot As String = My.Settings.removepath
        Dim strPrefix As String = My.Settings.removepnumber
        For Each strFolder As String In Directory.GetDirectories(strRoot)
            Dim newName As String = strFolder.Replace(strPrefix, "")
            '  Dim newName As String = strFolder.Replace(strPrefix, "")
            My.Computer.FileSystem.RenameDirectory(strFolder, newName)
        Next
    End Sub

   Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
       
	 removePrefix(My.Settings.removepnumber)

    End Sub

The error I am getting is at the Button_7 click event is:

Too many arguements to Private Sub removePrefix()

Any suggestions?

Thanks

daveofgv
 
Last edited:
For the removePrefix function, you will need two parameters, one for the root folder path and one for the prefix to remove. In the code above you only have one parameter (My.Settings.removepnumber), you will need two, ex:

VB.NET:
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    removePrefix(My.Settings.folderPath, My.Settings.removepnumber)
End Sub

That would be if you had another settings called "folderPath" that had the root folder path in it, ex "C:\Folder1".
 
I have two now, however, still same error

VB.NET:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        removePrefix(My.Settings.removepath, My.Settings.removepnumber)
    End Sub

My.Setting.removepath = directory path
My.Settings.removepnumber = prefix number

Here's everything I have:

VB.NET:
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Dim rootFolderPath As String = "C:\prioritytest\New folder"

        'Dim numberPrefix As String = "22_"

        'renameFolders(rootFolderPath, numberPrefix)
    End Sub

    Private Sub renameFolders(ByVal strRoot As String, ByVal prefixNumber As String)

        Dim subFolders() As String = Directory.GetDirectories(strRoot)

        Dim newname As String = ""

        For Each direct As String In subFolders

            newname = prefixNumber & IO.Path.GetFileName(direct)

            My.Computer.FileSystem.RenameDirectory(direct, newname)

        Next

        newname = prefixNumber & IO.Path.GetFileName(strRoot)

        My.Computer.FileSystem.RenameDirectory(strRoot, newname)

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim rootFolderPath As String = "C:\prioritytest\New folder"
        Dim rootFolderPath As String = My.Settings.pathtochange

        Dim numberPrefix As String = My.Settings.prioritynumber & "_"

        'Dim numberPrefix As String = "22_"

        renameFolders(rootFolderPath, numberPrefix)

    End Sub
    Private Sub removePrefix()
        Dim strRoot As String = My.Settings.removepath
        Dim strPrefix As String = My.Settings.removepnumber
        For Each strFolder As String In Directory.GetDirectories(strRoot)
            Dim newName As String = strFolder.Replace(strPrefix, "")
            '  Dim newName As String = strFolder.Replace(strPrefix, "")
            My.Computer.FileSystem.RenameDirectory(strFolder, newName)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        My.Settings.Save()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Folder As String
        'Specify the root folder this example is set to the MyComputer

        FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer

        Folder = FolderBrowserDialog1.ShowDialog()

        'Display the selected folder into a textbox

        TextBox1.Text = FolderBrowserDialog1.SelectedPath



    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Folder As String
        'Specify the root folder this example is set to the MyComputer

        FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer

        Folder = FolderBrowserDialog1.ShowDialog()

        'Display the selected folder into a textbox

        TextBox3.Text = FolderBrowserDialog1.SelectedPath

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        My.Settings.Save()

    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ' removePrefix(My.Settings.removepnumber)

        ' removePrefix("C:\Folder1", "02_")
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        removePrefix(My.Settings.removepath, My.Settings.removepnumber)
    End Sub
End Class
 
You have a slight mistake in your removePrefix code.

Replace: Private Sub removePrefix()
With: Private Sub removePrefix(ByVal strRoot As String, ByVal strPrefix As String)
 
Then when I type something into the text box it says:

Argument 'newName' must be a name, and not a relative or absolute path: 'C:\prioritytest\\1_77_prioritytest_01'. Parameter name: newName

and highlights:

VB.NET:
My.Computer.FileSystem.RenameDirectory(strFolder, newName)
 
Back
Top