Link to home
Start Free TrialLog in
Avatar of Samm1502
Samm1502

asked on

How to make the Dir() function case insensitive

Hi there

My application needs to get all files with the extension .DAT in a particular directory and at the moment the
call to DIr looks like this:

gstrDATFiles = gstrDirPath & "*.DAT"
   
'Now process all files
strFileName = Dir(gstrDATFiles, vbNormal)

and it does the job bt requires that all filenames be in upper case.  What I want is to get all
files ending .Dat or .DAT or .dat or even .dAt  so that I am not restricted by case.

Anyone any ideas.

Many thanks
    Sam
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

dim fso as object
set fso = createobject("scripting.filesystemobject")

dim folder as object
set folder = fso.getfolder(gstrDatfiles)

dim file as object
for each file in folder.files
  if ucase(file.name) like "*.DAT" then
     ...
  end if
next
The Dir$() or Dir() function is not case sensitive, so you should have no problem. Can you suggest that you check again?
ASKER CERTIFIED SOLUTION
Avatar of junglerover77
junglerover77

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are probably already stepping through a loop, but just in case you are not, this is what it should look like.

Sub WalkThroughFolder()
    Dim strFileName As String
   
    gstrDATFiles = gstrDirPath & "*.DAT"
    strFileName = Dir$(gstrDATFiles, vbNormal)
    Do Until strFileName = ""
        'process file  gstrDirPath & strFileName
        strFileName = Dir$()
    Loop
End Sub
Avatar of Naveen Swamy
Naveen Swamy

dir is case insensitive

Private Sub Form_Load()
    Dim sTemp As String
    sTemp = Dir(sTemp, vbNormal)
    While sTemp <> ""
        Debug.Print sTemp
        sTemp = Dir()
    Wend
End Sub
Avatar of Samm1502

ASKER

Many thanks - sorry for delay

This was handed to me as a problem but in fact it does process fileswith names of all cases.

Thanks!
You're welcome.