Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6842139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:03:46+00:00 2026-05-27T00:03:46+00:00

Using VBA with a reference to Microsoft Scripting Runtime. I’m trying to compare filenames

  • 0

Using VBA with a reference to Microsoft Scripting Runtime. I’m trying to compare filenames in two different directories, to find whether or not the same file is in both directories. However, I’m not looking for an exact match; I’d like to strip certain types of prefixes off, so, e.g., A-12234.pdf, 12234.pdf, and A_ 12234.pdf should be considered equal to each other.

Let’s say I have a function stripPrefix() that performs the necessary string operations to change a filename into a version that can be compared with others–in the prior case, stripPrefix() would return 12234.pdf for all 3. Then I could compare filenames in directories c:\dir1 and c:\dir2 using the following code:

sub findMatchFilenames()

dim fso as fileSystemObject
dim dir1 as folder
dim dir2 as folder
dim file1 as file
dim file2 as file

set fso=new fileSystemObject
set dir1=fso.getfolder("c:\dir1")
set dir2=fso.getfolder("c:\dir2")
for each file1 in dir1.files
 for each file2 in dir2.files
  if stripPrefix(file1.name)=stripPrefix(file2.name) then
   debug.print file1.name & " matches " & file2.name
  end if
 next file2
next file1

end sub

Big problem here: I’m calling stripPrefix() for the same filenames in dir2 every time I iterate the for..next file1 loop. I could build arrays dir1Array and dir2Array of stripped versions of each filename, but it seems I can’t correlate them with the Files collections in dir1 and dir2 because Files collections don’t have numerical indexes. I know because I tried using:

Set fso = New FileSystemObject
Set folderObj = fso.GetFolder("c:\data")
fileCount = folderObj.Files.Count
For c = 1 To fileCount
    MsgBox c & " " & folderObj.Files(c).name
Next

and got an “Invalid procedure call or argument” from folderObj.Files(c).name. I suppose I could increment a counter as I go in the for..next loops and use the counter to refer back to the array, like:

'after creating arrays of stripPrefix() for dir1 and dir2
ctr1=0
for each file1 in dir1.files
 ctr2=0
 for each file2 in dir2.files
  if dir1array(ctr1)=dir2array(ctr2) then
   debug.print file1.name & " matches " & file2.name
  end if
  ctr2=ctr2+1
 next file2
 ctr1=ctr1+1
next file1

but that seems really clunky. Is there a better way to handle this filename comparison operation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:03:47+00:00Added an answer on May 27, 2026 at 12:03 am

    Why not make a dictionary object of directory 2 and then search that using directory 1. You can strip off the prefixes when inserting into the dictionary object.

    Only problem is, you might not then have a unique filename in the dictionary

    For example A-12345 would be the same as A12345 after stripping the prefix

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using VBA, how can I: test whether a file exists, and if so, delete
Using ActiveX Data Objects 2.8 Library as a Reference from Excel VBA, and the
I'm using VBA in Excel and am trying to use the popular ShellAndWait subroutine.
I'm trying to save a PowerPoint presentation using VBA, and I want to save
I'm using VBA with Microsoft Access. I'm setting an object to an item inside
I'm using the the Microsoft.Jet.OLEDB.4.0 provider in Excel VBA ADO to connect to .csv
Using VBA i have a set of functions that return an ADODB.Recordset where all
Using VBA in MS Office, how do I add text to the Windows clipboard
I'm using VBA in the Excel VBE, but c# or vb are fine. The
The print functionality of Excel (using VBA) is extremely slow. I'm hoping someone has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.