Editing the lookup function for each argument.

MyHanhCB

New Member
Joined
Feb 20, 2023
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone on the forum!
I found on google a function as follows.
VBA Code:
Function FilesFoldersList(ByVal RootFolder As String, ByVal ListType As Boolean, _
                          ByVal Search As String, ByVal InSub As Boolean)
  Dim i As Long, fso As Object
  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim sComm As String, tmp As String, str As String, tmpFile ', arrA
  On Error Resume Next
  If Right(RootFolder, 1) <> "\" Then RootFolder = RootFolder & "\"
  str = """" & RootFolder & IIf(ListType, Search, "") & """"
  With fso
    tmpFile = .GetTempName
    sComm = "DIR " & str & " /ON /B /A" & IIf(ListType, "-", "") & "D-S" & IIf(InSub, "/S", " ") & " >" & tmpFile
    CreateObject("Wscript.Shell").Run "cmd /u /c " & sComm, 0, True
    With .OpenTextFile(tmpFile, 1, , -2)
      tmp = Trim(.ReadAll)
      If Right(tmp, 2) = vbCrLf Then tmp = Left(tmp, Len(tmp) - 2)
      If Len(tmp) Then
        If InSub = False Then tmp = RootFolder & Replace(tmp, vbCrLf, vbCrLf & RootFolder)
        FilesFoldersList = Split(tmp, vbCrLf)
      End If
      '-------------------------------------------------
      .Close
    End With
  End With
  Kill tmpFile
End Function


I do the following search.....

Code:
Sub test
Dim fldPath as string, arr, db as variant
fldPath = "C:\Users\toanndq.VBD\Desktop\New folder (3)"
Arr = FilesFoldersList(fldPath, True, "CX*44??.pdf", True)
For each db in arr
Debug.Print db
Next
End sub
But the value after my search gets both
"CX123_E44a.pdf, CX123_E44b.pdf,CX123_E44aa.pdf,CX123_E44bb.pdf,CX123_E44aaa.pdf,CX123_E44bbb.pdf"
I want the find value to be just "CX123_E44aa.pdf,CX123_E44bb.pdf"
Look forward to the help
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Your routine is calling the DIR command
This is caused by the DIR command not Excel VBA.
Given the following files:

k1.txt
k11.txt
k111.txt

DIR k?.txt returns K1.txt
DIR k??.txt returns both k1.txt and k11.txt which is the problem you are getting.

Google "windows DIR wild characters" and see if there is a way of making the ? character limited to the number of wildcharacters you are searching for.

I can't see anything obvious at present :(
 
Upvote 0
DIR k?.txt returns K1.txt
DIR k??.txt returns both k1.txt and k11.txt which is the problem you are getting.
Yes, that's the problem I'm having. Is there any way to find the correct value (CX*44??.pdf ==> CX123_E44bb.pdf, CX123_E44aa.pdf)
The example I gave in the article above is a bit confusing. Sorry everyone
 
Upvote 0
Am wondering if Regular Expressions may solve this?

Alternatively, pipe the DIR results to a text file and use VBA to analyse that file for the pattern you're searching for.
 
Upvote 0
Am wondering if Regular Expressions may solve this?

Alternatively, pipe the DIR results to a text file and use VBA to analyse that file for the pattern you're searching for.
Yes, thanks bro, I found the solution, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top