Question

In the code below I could wrote a code to display filenames from a folder. The problem is now that I should display them in the correct Row. For now they are displayed in random sequence and that is not the purpose.

In column "A" the filenames to search for are called with similar name format PBM12.T5.103.

The actual filename to find is called with similar name format 1_29_PBM_12_T5__103.

I have to find a solution to compare "only" the Fat marked letters and numbers like displayed here above, without . or _

As you will see PBM12T5103 is returning in both namestructures.

Please don't try fixed length counts because the filenames are dynamic and the amount of letters are variable. The comparison of the SUBSTITUTED length of column "A" ( PBM12T5103) is the key to comparison but I can not handle to establish this comparison.

When the filename in column "A" has been found, in column "C" the full filename of found file has to be displayed as the original format 1_29_PBM_12_T5__103

Maybe a solution can be found when extra columns can be made to establish the comparison?

In Excel I could approach a solution, but this will not work automized like it should do.

I made the LEN(count dynamic), but this is still no solution to display the full filenames in the required row...

Hopefully somebody can help me out ..

Option Explicit
Sub fileNames_in_folder()
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Dim fldpath
Dim fld As Object, fil As Object, fso As Object, j As Long
 fldpath = "C:\"
  On Error Resume Next
   Thisworkbook.Sheets("1").Activate
   'start count row
    j = 11
Set fso = CreateObject("scripting.filesystemobject")
Set fld = fso.getfolder(fldpath)
   For Each fil In fld.Files
'here I have to add an IF statement in order to compare the filenames written in column "A" with files from folderPath C:\" 
'When the correct files is found it should be displayed in column "C"
    If


   then
Cells(j, 34).Value = fso.GetBaseName(fil.path)
   End If    

'count behaviour
    j = j + 1
  Next
Columns("AH").AutoFit
End Sub
Was it helpful?

Solution

I will suggest you different way of getting file names. Instead of FileSystemObject let's use simple Dir function which allows to check the pattern of file name.

1) Files in my testing folder are as follows

enter image description here

2) I assumed that file pattern is as follows:

XXXY.Z.W

where:

XXX > 3 letters text 
Y > any length number/text 
Z > any length number/text 
W > any length number/text

3) The code of subroutine is placed in 2013-06-01...xlsm file which you could see in the pic above (the same folder where your files are). Code is as follows (change where appropriate):

Sub solution()

    Dim j As Long, LastRow As Long
    Dim fldPath
        'your path below
        fldPath = ThisWorkbook.Path
        ChDir fldPath

    Dim arrPattern As Variant
    Dim filName As String

    For j = 1 To Range("A1").End(xlDown).Row
        arrPattern = Split(Cells(j, "A"), ".")

        'I suggest to use different way of checking _
        pattern of file name. Pattern rules:
        '*YYY*XX*Z*W*

        filName = Dir("*" & Left(arrPattern(0), 3) & "*" & _
                            Mid(arrPattern(0), 4) & "*" & _
                            arrPattern(1) & "*" & _
                            arrPattern(2) & "*")
        If Len(filName) > 0 Then
            Cells(j, "B") = filName
        Else
            Cells(j, "B") = "not found"
        End If


    Next j

End Sub

4) results are presented in the picture below:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top