PDA

View Full Version : Using VBA to rename or delete subfolders



rey06
01-04-2022, 11:57 AM
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

大灰狼1976
01-05-2022, 06:22 AM
Hi rey06!
like this:

RmDir "fullpath of folder"


--Okami

rey06
01-05-2022, 06:41 AM
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!

大灰狼1976
01-05-2022, 06:49 AM
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

snb
01-05-2022, 07:01 AM
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

rey06
01-05-2022, 07:29 AM
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.

Paul_Hossler
01-05-2022, 07:53 AM
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

rey06
01-05-2022, 08:25 AM
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!

Paul_Hossler
01-05-2022, 09:02 AM
The macro works for me

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

Else post some more information and a workbook

29281