Results 1 to 8 of 8

Macro to check C: Drive to determine when any duplicate folders

Threaded View

Howardc1001 Macro to check C: Drive to... 11-25-2024, 02:02 AM
mjr veverka Re: Macro to check C: Drive... 11-25-2024, 07:08 AM
Howardc1001 Re: Macro to check C: Drive... 11-25-2024, 10:03 AM
ByteMarks Re: Macro to check C: Drive... 11-26-2024, 08:35 AM
Howardc1001 Re: Macro to check C: Drive... 11-26-2024, 09:55 AM
AliGW Re: Macro to check C: Drive... 11-26-2024, 10:01 AM
Howardc1001 Re: Macro to check C: Drive... 11-26-2024, 10:11 AM
Howardc1001 Re: Macro to check C: Drive... 11-26-2024, 10:13 AM
  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,890

    Macro to check C: Drive to determine when any duplicate folders

    I need to determine whether I have any duplicate folders on my C:\Drive including duplicate folders within sub-folders


    I have tried to write code to do this, get message "File not responding"

    It would be appreciated if someone could kindly amend my code

     Sub ListDuplicateSubfolders_MacroSheet()
        Dim ParentFolder As String
        Dim ExcludeFolders As Variant
        Dim FSO As Object
        Dim Folder As Object
        Dim SubFolder As Object
        Dim FolderDict As Object
        Dim ws As Worksheet
        Dim DuplicateFolder As Variant
        Dim OutputRow As Long
        
        ' Specify the root folder to scan (C:\)
        ParentFolder = "C:\"
        
        ' Excluded folders (case-insensitive)
        ExcludeFolders = Array("Windows", "Program Files", "Program Files (x86)", "System Volume Information", "$Recycle.Bin")
        
        ' Initialize FileSystemObject and Dictionary
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set FolderDict = CreateObject("Scripting.Dictionary")
        
        ' Reference the "Macro" sheet
        On Error Resume Next
        Set ws = ThisWorkbook.Sheets("Macro")
        On Error GoTo 0
        If ws Is Nothing Then
            MsgBox "Sheet 'Macro' not found!", vbExclamation
            Exit Sub
        End If
        
        ' Clear column D from D1 to the last row
        ws.Columns("D").ClearContents
        
        ' Get root folder
        On Error Resume Next
        Set Folder = FSO.GetFolder(ParentFolder)
        On Error GoTo 0
        If Folder Is Nothing Then
            MsgBox "The specified folder does not exist: " & ParentFolder, vbExclamation
            Exit Sub
        End If
        
        ' Recursively scan subfolders with error handling
        Call ScanSubfoldersWithErrorHandling(Folder, FolderDict, ExcludeFolders)
        
        ' Output duplicates to column D
        OutputRow = 1 ' Start at the first row in column D
        For Each DuplicateFolder In FolderDict.Keys
            If InStr(FolderDict(DuplicateFolder), vbCrLf) > 0 Then
                ws.Cells(OutputRow, 4).Value = DuplicateFolder ' Column D for folder names
                ws.Cells(OutputRow, 5).Value = FolderDict(DuplicateFolder) ' Column E for folder locations
                OutputRow = OutputRow + 1
            End If
        Next DuplicateFolder
        
        ' Message if no duplicates found
        If OutputRow = 1 Then
            MsgBox "No duplicate folders found on C:\.", vbInformation
        Else
            MsgBox "Duplicate folders listed in column D of sheet 'Macro'.", vbInformation
        End If
    End Sub
    
    Sub ScanSubfoldersWithErrorHandling(Folder As Object, FolderDict As Object, ExcludeFolders As Variant)
        Dim SubFolder As Object
        
        On Error Resume Next ' Skip inaccessible folders
        For Each SubFolder In Folder.SubFolders
            If Err.Number <> 0 Then
                Err.Clear ' Clear the error and continue
                GoTo NextSubFolder
            End If
            
            If Not IsExcluded(SubFolder.Name, ExcludeFolders) Then
                If Not FolderDict.Exists(SubFolder.Name) Then
                    FolderDict.Add SubFolder.Name, SubFolder.Path
                Else
                    FolderDict(SubFolder.Name) = FolderDict(SubFolder.Name) & vbCrLf & SubFolder.Path
                End If
                ' Recursively scan further subfolders
                Call ScanSubfoldersWithErrorHandling(SubFolder, FolderDict, ExcludeFolders)
            End If
    NextSubFolder:
        Next SubFolder
        On Error GoTo 0 ' Reset error handling after loop
    End Sub
    
    Function IsExcluded(FolderName As String, ExcludeFolders As Variant) As Boolean
        Dim Exclude As Variant
        For Each Exclude In ExcludeFolders
            If LCase(FolderName) = LCase(Exclude) Then
                IsExcluded = True
                Exit Function
            End If
        Next Exclude
        IsExcluded = False
    End Function
    Last edited by Howardc1001; 11-25-2024 at 03:55 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy folders from Shared Drive to SharePoint
    By E5254730 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2023, 04:39 AM
  2. Folders name of X Drive listed on Excel
    By waqarqrl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2015, 05:43 AM
  3. [SOLVED] Find the Empty Folders -Particular path/Drive
    By laxmanann in forum Excel Programming / VBA / Macros
    Replies: 32
    Last Post: 06-06-2014, 10:57 PM
  4. [SOLVED] Check Properties of folders within drive
    By TonyforVBA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2012, 09:58 AM
  5. INDEX sheet of all the files and folders in a drive.
    By all4excel in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2007, 03:08 PM
  6. Drive - Folders - Files
    By DPC in forum Excel General
    Replies: 1
    Last Post: 05-26-2005, 05:15 PM
  7. creating folders on hard drive from name field
    By Italian Pete in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2005, 06:48 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1