+ Reply to Thread
Results 1 to 11 of 11

Excel VBA: Search folder and sub-directories for files matching partial string

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Excel VBA: Search folder and sub-directories for files matching partial string

    I have a sub that's part of a larger routine to search folders and subfolders. I want to integrate a script that once a subfolder is found, to look within the subfolder and find files that contain the letters *dq* within it. I've attempted to try to use a couple approaches and have come up with this as my best option, although when I run it, no errors are thrown and no output occurs even though there are numerous files within the main directory meeting the criteria. My objective is when a file is found within the subfolder to open and search for a string and if true then list the file name and location in the sheet, then proceed to the next file within the subfolder.
    Please Login or Register  to view this content.
    EDIT: after looking at the code, I realized I had a redundant search. I revised to look at each File within the loop, however it still does not find any existing files within the directory
    Last edited by terriertrip; 10-16-2020 at 01:43 PM. Reason: incorrect code; revised code

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    Application.FileSearch isn't available any more, I think it was stopped in Excel 2000.

    Can you post the code you are calling this code from?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    Thanks, Norie. I just read about that and I also revised the code. Here is the initial sub:
    Please Login or Register  to view this content.
    The relative functions:
    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    Two things.

    1 You need to look at the Name property of File.

    2 To search using wildcards you need to use Like.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    I've revised the OP code and removed the unnecessary inner loop. When it finds the file, it starts the procedure to carry the value to the worksheet, but then exits to the GetSubFolders func and resumes the next search. Even if I change it to Folder.Path it still jumps back into the GetSubFolders func.

  6. #6
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    I've attached the workbook with code.
    Attached Files Attached Files

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    How many levels of subfolders are there?

  8. #8
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    In this example, there are 3-4. For the final project, there could be several deep.

  9. #9
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    I think I'm going to use the Recursive Folder Search from Ammara Digital Image Solutions. That seems to be producing the output I need. I think I can retrofit it to do what I need it to do.

  10. #10
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    Tangent: this is something the OS generally does better than VBA even using Windows Script Host objects.

    For what it's worth, the following batch file produces CSV output with the desired information.

    Please Login or Register  to view this content.
    My point in mentioning this is that you could keep batch files in ranges in very hidden worksheets, write them to disk and run them as needed, redirecting output to files when running them, opening or importing the files created, and finally deleting those ad hoc files.

    Going on a different tangent, you could include a VBA project reference to the Windows Script Host (WSH), namely, scrrun.dll, to make its classes' properties and methods available in the VBA Editor as if they were built-in classes. Then you could use

    Please Login or Register  to view this content.
    as well as using the actual object types for all the WSH classes.

  11. #11
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel VBA: Search folder and sub-directories for files matching partial string

    Fascinating. I have a very rudimentary understanding of batch files. I only use them strictly for backing up my directories. haha. I've seen some examples on SO and I have much to learn.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Search for a string on all excel files inside a ZIp folder
    By Moreno20 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2014, 03:45 PM
  2. How To Use a ComboBox to Search Matching Files Name & Open it From A Folder
    By DKTRL in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2013, 08:34 AM
  3. [SOLVED] Search for a partial string match and cycle through matching results
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2013, 08:27 AM
  4. copy files located in multiple directories that match a partial value
    By msheen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2012, 04:55 PM
  5. Want to search for string in list of files in a folder
    By DeepthiReddy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2012, 03:21 AM
  6. Case insensitive search for string within folder of excel files
    By vba_novice in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-22-2011, 11:21 PM
  7. Search files in folder based on search string
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-22-2008, 01:12 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