Consulting

Results 1 to 9 of 9

Thread: Using VBA to rename or delete subfolders

  1. #1
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location

    Using VBA to rename or delete subfolders

    Hello,

    I have a macro that will create subfolders at a specified location. There were a couple hundred today that were named incorrectly. I've found some code that is said to delete folders specified in a certain column (like I put the file paths in column A that I want deleted) but nothing is working. I'd like to either rename these somehow or delete them. It's really not the end of the world - the folder was named "old folders" instead of "old files" but I'd still like to correct.

    I'm wondering if there is an easy way to use a macro to do this? Almost like a find and replace, like have current name in column A, desired name in column B, or just a delete the folders noted in column A? Re-creating these is no problem as I can do it in seconds with the current macro.

    I know there are existing answers for this, and I've tried several different methods and nothing is working.

    TIA!

    R

  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi rey06!
    like this:
    RmDir "fullpath of folder"

    --Okami

  3. #3
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by 大灰狼1976 View Post
    Hi rey06!
    like this:
    RmDir "fullpath of folder"

    --Okami
    Hi Okami,

    Sorry, I'm not following this. If I have the full paths in column A, how would the code look?

    Thank you!

  4. #4
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Assumed that the list of full paths is in Sheet1!A1:A10, something like below.
    Sub test_1()
    Dim i As Long
    For i = 1 To 10
      RmDir Sheet1.Cells(i, "A")
    Next i
    End Sub
    --Okami

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why using macros if you haven't got the faintest idea ?

    Example:
    Sub M_snb()
       CreateObject("scripting.filesystemobject").GetFolder("G:\OF\AA1").Move "G:\OF\AA10"
    End Sub
    Last edited by snb; 01-05-2022 at 07:12 AM.

  6. #6
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by snb View Post
    Why using macros if you haven't got the faintest idea ?

    Example:
    Sub M_snb()
       CreateObject("scripting.filesystemobject").GetFolder("G:\OF\AA1").Move "G:\OF\AA10"
    End Sub
    Wow, there's no need to be rude. I'm asking because I'm trying to learn more.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    VBA way


    Option Explicit
    
    
    Sub RenameFolders()
        Dim r As Range
        
        With Worksheets("Sheet1")
            For Each r In .Cells(1, 1).CurrentRegion.Rows
                On Error GoTo Oops
                Name r.Cells(1, 1).Value As r.Cells(1, 2).Value
                On Error GoTo 0
            Next
        End With
    
    
        MsgBox "Done"
    
    
        Exit Sub
        
    Oops:
        MsgBox "Error renaming " & r.Cells(1, 1).Value & " --> " & r.Cells(1, 2).Value
        Resume Next
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    For whatever reason, none of these can find the file path I've provided in column A. I've tried it with and without the forward slash at the end of it. I know the file path is good because I have another macro that uses almost the exact same file path to pull in the names of all the files in folders, it just excludes those that I'm trying to create. Is this something that could be because it's on a network drive?

    I appreciate all of the suggestions here though!

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    The macro works for me

    Maybe you have to clean your data: remove trailing spaces, etc.

    Else post some more information and a workbook

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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