VBA delete list of folders in Excel spreadsheet

698 Views Asked by At

I am using an application called "All Dup" to find duplicate files, normally matching by CRC checksum. Once I have run the scan I am left with a list of about 10000 duplicate files. I can then export the results as a csv file. What I would like to do is have a Macro that will go and delete all the specified folders in a column, i.e, I have all the folders in column A.

I know how to delete a single directory, but would really appreciate some help with a "loop" function or something along those lines.

1

There are 1 best solutions below

5
AudioBubble On

This loop will walk down column A from row 2 to the last populated row. Each cell is first examined to see if it is non-blank then it is examined to see if it exists as a folder with the Dir function.

With Worksheets("Sheet1")
    For dr = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
        If CBool(Len(.Cells(dr, "A").Value2)) Then
            If CBool(Len(Dir(.Cells(dr, "A").Value2, vbdirectrory))) Then
                Kill .Cells(dr, "A").Value2 & Chr(92) * Chr(42)
                RmDir .Cells(dr, "A").Value2 
            End If
        End If
    Next dr
End With

If both tests pass, then the folder is removed with the Kill function. If the folder is populated with files, they should be removed first.