VBA code to find file names within a folder on local drive

gogazapr

New Member
Joined
Jun 21, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have been given a large list of file names in excel worksheet. I have to find which of those files DO NOT exist in a folder on my local drive. Is there a VBA code I can write to perform this operation?
Note: most of the files are in the folder, I have to identify those that are not.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This macro expects the file names in column A starting at A2 and puts the results in column B.
VBA Code:
Public Sub Find_Files_In_Folder()

    Dim sourceFolder As String
    Dim fileNames As Variant
    Dim fileNameCell As Range
    
    sourceFolder = "C:\path\to\folder"    'CHANGE FOLDER PATH
    
    fileNames = "|" & Replace(CreateObject("WScript.Shell").Exec("cmd /c DIR /B /A-D """ & sourceFolder & """").StdOut.ReadAll, vbCrLf, "|")
    
    With ActiveSheet
        For Each fileNameCell In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
            If InStr(1, fileNames, "|" & fileNameCell.Value & "|", vbTextCompare) Then
                fileNameCell.Offset(, 1).Value = "Found"
            Else
                fileNameCell.Offset(, 1).Value = "Not found"
            End If
        Next
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top