Matching file names in Excel column to file names in folder

Copper Contributor

Hi,
I have a column in excel that contains the file names of files that are in a folder (ex: IMG_001, contacs.pdf, essay.docx). I have about 30 rows with miscellaneous file names in the column, and the folder contains over 1,000 varied files. What I'm looking to accomplish is to use the file names in the Excel column to find the file names in the folder, similar to matching file names between two spreadsheets with VLOOKUP. This will allow me to find the files without having to sift through the entire folder to find specific files or type each file name into the folder search bar.

I tried searching for online for this task but only managed to find how to export file names from a folder to Excel. This task is almost the opposite, being that I already have the file names in Excel and need to find the file names in a folder.

Additionally, I'm not too sure if I'm in the correct conversation community, but since I'm starting with an Excel workbook, I figured I'd start here and hopefully find direction.

Please help!

15 Replies

@Ozz_Kozz 

Can you upload a sample file so that we can see what you are trying to achieve?

Does the column have a full path of the file or only file names? Does any column or cell contains the folder path to look for those files?

And if the file is found in the target folder, what do you want to do then? Insert a VLookup Formula referring to those file names in the formula?

A sample file will help to find the answer to the above queries.

@Subodh_Tiwari_sktneerHi Thank you for your reply. I'll post a picture and hopefully it can come across clearer. In the example below there are 7 clients with file names in column D and 38 files in the folder. I only need to target the 7 files. The only criteria I have is the file name. What I would do is copy and paste the file name from the spreadsheet into the folder search box. That would work if I were only looking for 7 files by name in a folder with 38 files, but in real life I have many more file names in my spreadsheet and over 1,000 files in the folder. I compared it to VLOOKUP because if the folder were a spreadsheet, I could easily create a column and run the formula to match the files based on the name. Essentially I would want to be able to either highlight the names or somehow extract the files from the large folder. Please let me know if this make sense and if I'm on the right track. Thank you!files in folder_1.jpg

@Ozz_Kozz 

Let me know if I understood your requirement.

All you want to do is, check whether the files listed in column D are found in the Document folder.

e.g. Check if the file tps report.xls listed in cell D2 is found in the Document folder and if it exists in that folder, highlight the cell D2 which would be like a visual indicator that this file was found in the folder and do the same for all the files in column D.

Is that what you are trying to achieve?

@Subodh_Tiwari_sktneer Hi, yes but if exist in the Document Folder it should be highlighted in the Document Folder. I'm trying to avoid typing in the file name into the search box of the Document folder and save some time. I would want to find the file and extract it from the large folder. Hope that explains a bit more. Thanks.

@Ozz_Kozz 

Okay, please try the following code and see if this is something you can work with.

The following code will check the files listed in column 4 of the Table on Sheet called "B" in the Document folder and if it finds the file in that folder, it will highlight the cell with filename in the Table and copy the file from Document folder to a Sub-Folder called "Found Files".

 

Sub SearchFiles()
Dim ws                      As Worksheet
Dim tbl                     As ListObject
Dim cel                     As Range
Dim rootFolder              As String
Dim strNameNewSubFolder     As String
Dim fso                     As FileSystemObject
Dim newFolder               As Folder
Dim fil                     As File
Dim strFilepath             As String
Dim newFilePath             As String

Set fso = New FileSystemObject
Set ws = Worksheets("B")
Set tbl = ws.ListObjects(1)

'Path of the Source folder with files
rootFolder = "C:\Users\sktneer\Documents"

If Not fso.FolderExists(rootFolder) Then
    MsgBox rootFolder & " doesn't exist.", vbExclamation, "Source Folder Not Found!"
    Exit Sub
End If

'files that are found in the Source Folder would be copied to this New Sub-Folder
'Change the name of the Sub-Folder as per your requirement
strNameNewSubFolder = "Found Files"

If Right(rootFolder, 1) <> "/" Then rootFolder = rootFolder & "/"

If Not fso.FolderExists(rootFolder & strNameNewSubFolder) Then
    fso.CreateFolder rootFolder & strNameNewSubFolder
End If

Set newFolder = fso.GetFolder(rootFolder & strNameNewSubFolder)

tbl.DataBodyRange.Columns(4).Interior.ColorIndex = xlNone

For Each cel In tbl.DataBodyRange.Columns(4).Cells
    strFilepath = rootFolder & cel.Value
    newFilePath = newFolder.Path & "/" & cel.Value
    If fso.FileExists(strFilepath) Then
        cel.Interior.Color = vbYellow
        Set fil = fso.GetFile(strFilepath)
        'The following line will copy the file found to the newly created Sub-Folder
        fil.Copy newFilePath
    End If
Next cel
Set fso = Nothing
End Sub

Just forgot to tell you that you will need to add a reference to a library called "Microsoft Scripting Runtime".

 

To do so, go to Tools on VB Editor's Ribbon --> References --> search for the library called "Microsoft Scripting Runtime" and check the CheckBox next to it and click OK to finish.

@Subodh_Tiwari_sktneer Thank you for your reply. Where would I enter this code? And can I undo this after I'm done with the task?

@Ozz_Kozz 

You should place this code on a Standard Module like Module1 and to do so follow these steps...

 

  1. Open your file and then press Alt+F11 to open VB Editor.
  2. On VB Editor Ribbon -->go to Insert Tab --> and choose Module. This will insert a new Module called Module1 and open a code window.
  3. Paste the code into the opened code window, close the VB Editor.
  4. Save your file as Macro-Enabled Workbook.
  5. On the sheet with data, insert a Shape or a Button from Form Controls, right click to it and choose Assign Macro and then choose the macro SearchFiles from the available macro list and click OK to finish.
  6. Now you may click this shape or button to run the code.

Please find the attached in which I have placed the code on Module1 and inserted a button called "Search Files" on the Sheet. You may click this button to run the code.

 

No, you cannot undo the steps performed by the macro. Btw what is there which you want to undo after the code execution?

 

In the attached, I have also tweaked the path of the source folder. Now the code assumes that you have a folder called "Document folder" with all the files on your Desktop.

 

The tweaked line is as below...

rootFolder = Environ("UserProfile") & "\Desktop\Document Folder"

 

 

@Ozz_Kozz 

 

 With the permission of all involved, in addition, for example,

from Mr.Subodh Tiwari (@Subodh_Tiwari_sktneer) with a small example file that I found on the Internet.
  

Read out the file name of a directory with VBA and write it in Excel
Source: January 31, 2017 by Marco Schade
https://www.makro-excel.de/2017/01/31/dateinamen-eines-rechner-mit-vba-auslesen-und-in-excel-schreib...



With a small macro you can easily write all file names including the file extensions in Excel. You only need to add the following macro to a module of your Excel file. Alternatively, you can also use the sample file attached to this article to read file names with VBA.

There are two important points to consider before you start. Before running the macro, you have to change the folder name in the VBA code. In the example, the macro would otherwise search for the folder with the name “Folder Name” in the “C: \” drive and, if available, list the files in this folder in column A from row 1. If the folder path does not exist, you will receive the error message "Runtime error’ 76 ‘: Path not found".
Runtime error 76
Read out file names with VBA

Also make sure that there is no content in column A in the table, otherwise it may be overwritten! Would you like the file names not from line 1, but e.g. only write from line 5. Also change the code in the VBA code from

lngline = 1

on

lngline = 5

from.

All of the source code for this macro is as follows:

List Sub Files ()

Dim lngLine As Long
Dim objFileSystem As Object
Dim obj Directory As Object
Dim obj List of files As Object
Dim obj file As Object

Set objFileSystem = CreateObject ("scripting.FileSystemObject")
Set objDirectory = objFileSystem.GetFolder ("C: \ folder name")
Set objFileList = objDirectory.Files

lngline = 1

For Each objfile In objfilelist
If Not objFile Is Nothing Then
ActiveSheet.Cells (lngLine, 1) = objFile.Name
lngline = lngline 1
End If
Next obj file

End Sub

If you do not want to write all file names in Excel, you can also exclude certain file extensions or file names with a further small modification. To do this, change the line with the VBA code:

If Not objFile Is Nothing Then

in

If Not objFile Is Nothing And Right (LCase (objFile.Name), 4) = ".jpg" Then

from.

The addition And Right (LCase (objDatei.Name), 4) = ".jpg" leads to the restriction that only files with the ending ".jpg" are written in Excel. If you want to write all file names except a certain extension in Excel, add the command “Not”. Example: And Not Right (LCase (objDatei.Name), 4) = ".jpg"

The macro presented here only reads the file names of the current folder. The names of sub-folders and the files in them are not taken into account. The files in sub-folders can of course be also be taken into account. In this case, however, a somewhat more extensive change to the VBA source code is required.
Read files from subdirectories

In order to be able to read the file names from sub-folders, an additional procedure is required which is called for each individual sub-folder. Because the files in the main folder are not taken into account in this example, the two macros may have to be combined. To do this, call the second procedure at the end of the first macro using the command

Call Read out subfolder (objDirectory)

on. The additional VBA procedure required is then called via this program line.

Sub Subfolder readout (ByVal strFile path As String)

Dim objFileSystem As Object
Dim obj Directory As Object
Dim obj subfolder As Object
Dim obj file As Object
Dim i As Long

Set objFileSystem = CreateObject ("Scripting.FileSystemObject")
Set objDirectory = objFileSystem.getfolder (strFilePath)

If Cells (Rows.Count, 1) .End (xlUp) .Row> 1 Then
i = Cells (Rows.Count, 1) .End (xlUp) .Row 1
Else
i = 1
End If

For Each objSubfolders In objDirectory.subfolders
For Each objFile In objSubfolder.Files
If Not objFile Is Nothing And Not Right (LCase (objFile.Name), 4) = ".jpg" Then
ActiveSheet.Cells (i, 1) = objFile.Name
ActiveSheet.Cells (i, 2) = objSubfolder.Path
i = i 1
End If
Next obj file
Call UnterFolderAuslesen (objUunterfolder.Path)
If Cells (Rows.Count, 1) .End (xlUp) .Row> 1 Then
i = Cells (Rows.Count, 1) .End (xlUp) .Row 1
Else
i = 1
End If
Next obj subfolder

End Sub

 

I hope that you will get on with your project with this additional information.

Wish everyone involved a nice day / night with health, joy and love.

 

Nikolino

I know I don't know anything (Socrates)

@Subodh_Tiwari_sktneer Thank you for your detailed response! I will give that a try and circle back if I run into any errors. 

@NikolinoDE Thank you to you as well, this is all great info, and surely helpful. Thanks again!

@Subodh_Tiwari_sktneer How can i modify the code from your example file that can i search just a part of the filename not the exact filename?

If in the search folder i have the file named 111;222;333.pdf if i search 222 the result should be exactly like i search the entire filename.

Hello! - I would be happy to check with you the possibility of adjusting the code. Because in the current configuration, it does not really work for me.

I need the code to scan all the cells in column A, and if there is a match between the information in the cell and the same file name in the original folder where the excel file is saved, the code will create one new folder, somewhere changeable, and copy all files with a name similar to the data in each cell In column A for the location in the new folder.
Hello, everyone. I've been trying to use this macros with no luck. I've already modify path to a folder located in my desktop, names my worksheet after the name, changed colour number but Im only able to creat a folder inside the destination folder. What could possibly is going wrong?
I'm very new to this, so thank you for what you've provided so far! I'm wondering if someone could help me tweak some things, as I can't seem to get the code to work...

I've gotten it to create the subfolder in the source folder, but I would like to have the program search for partial file names instead of whole names (ex: search for 20448 results in the file labeled: 20448 xxxx-xxxxxxxxxx, xxx-xxx, xxxxx-xx)

I'm wondering what I would need to add to this code to have it return the correct files with only the digits at the beginning of the file name.

Thanks in advance!