Reputation: 9546
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?
Upvotes: 1
Views: 1225
Reputation: 3835
Why don't you use the use of wildcards through using DIR?
Something like (not tested):
myFile = Dir("C:\*1234.pdf")
Do While myFile <> ""
If myFile <> "." And MyFile <> ".." Then
Msgbox myFile
End If
MyFile = Dir
Loop
Put this into an array. And you can do the same for the other directory as well, then compare the two arrays.
Upvotes: 1
Reputation: 1183
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
Upvotes: 1