Results 1 to 8 of 8

Thread: VBA req to rename the folders,subfolders & its files, if it contains any special char

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    VBA req to rename the folders,subfolders & its files, if it contains any special char

    Hi Friends,
    i have a macro, which it will fetch all the folders,subfolder & files into an excel & it will remove all special characters.
    I need a help on rename the folder,subfolder & its file names if it contains any special characters (@,#,$) at the begining(only at the starting,not in middle or end).
    for better understanding please check the attached snapshot.
    Please help me to complete this Task.

    Name:  folder names.JPG
Views: 1598
Size:  70.4 KB

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Welcome to VBForums

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Muahahaha.
    Exactly the same here.
    http://www.vbforums.com/showthread.p...and-file-names
    Looks like an assignment in IT-classes or something.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    http://www.vbforums.com/showthread.p...ht=rename+file
    another recent version of the same thing, plenty of reading!!
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Eh? That's the Thread with my Hardcore-Stab at it
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Hi Friends,
    i am using the below code, which is even renaming the folders in Network drives, but its not renaming the sub folders & its files, so could anyone help me to fix this program, as i need it to remove special characters from all subfolders & its files.

    Code:
    Sub File_renaming2()
        Dim objFSO As FileSystemObject, myObj As Object, mySource As Object, Folder As Variant
        Dim newName As String
    
        Set objFSO = New FileSystemObject
        Set mySource = objFSO.GetFolder("\\Server01\user\docs\new folder")
    
        For Each Folder In mySource.SubFolders
            newName = Folder.Name
            newName = Replace(newName, "@", " ")  'replace - with space
            newName = Replace(newName, "#", " ")  'replace : with space
            newName = Replace(newName, "$", " ")  'replace ; with space
            newName = Replace(newName, "+", " ")  'replace / with space
            If Folder.Name <> newName Then Folder.Name = newName ' assign new name. the check is to (possibly) ensure you're not processing the same folder twice
        Next Folder
    End Sub

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Code:
    as i need it to remove special characters from all subfolders & its files.
    you can not rename the folders sub folders and files in one command, you need to rename each folder and file specifically in turn, that is you need a recursive procedure, whether you use FSO or vba i/o

    Code:
    Sub File_renaming2()
        Dim objFSO As FileSystemObject, myObj As Object, mySource As Object, Folder As Variant
        Dim newName As String
    
        Set objFSO = New FileSystemObject
        Set mySource = objFSO.GetFolder("\\Server01\user\docs\new folder")
       recurserename mysource
    
    End Sub
    
    Sub recurserename(f As Folder)
    Dim itm As Folder, itm2 As File
    
            For Each itm In f.SubFolders
            recurserename itm
            newName = itm.Name
            newName = Replace(newName, "@", " ")  'replace - with space
            newName = Replace(newName, "#", " ")  'replace : with space
            newName = Replace(newName, "$", " ")  'replace ; with space
            newName = Replace(newName, "+", " ")  'replace / with space
            If itm.Name <> newName Then
                itm.Name = newName ' assign new name. if changed
            End If
        Next itm
    
        For Each itm2 In f.Files
            newName = itm2.Name
            newName = Replace(newName, "@", " ")  'replace - with space
            newName = Replace(newName, "#", " ")  'replace : with space
            newName = Replace(newName, "$", " ")  'replace ; with space
            newName = Replace(newName, "+", " ")  'replace / with space
            If itm2.Name <> newName Then
                itm2.Name = newName ' assign new name. if changed
            End If
        Next itm2
        
    End Sub
    make sure to test fully before use, if i got it right, it should rename all files and folders in mysource and below
    Last edited by westconn1; May 12th, 2018 at 05:31 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: VBA req to rename the folders,subfolders & its files, if it contains any special

    Hi Westconn,
    Much appreciated!!! Amazing work done by you.
    It worked like a charm Tons of thanks to you!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width