How to make the Dir() function case insensitive

Posted on 2006-05-16
Medium Priority
Last Modified: 2010-05-01
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
Question by:Samm1502
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16690263
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
LVL 76

Expert Comment

ID: 16692434
The Dir$() or Dir() function is not case sensitive, so you should have no problem. Can you suggest that you check again?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

junglerover77 earned 2000 total points
ID: 16695360
Hi Sam, Dir is not case sensitive but it will ONLY return 1 file name.

So, to use FSO is a good idea for this purpose.

LVL 76

Expert Comment

ID: 16696718
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$()
End Sub

Expert Comment

by:Naveen Swamy
ID: 16697082
dir is case insensitive

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

Author Comment

ID: 16716996
Many thanks - sorry for delay

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

LVL 76

Expert Comment

ID: 16730627
You're welcome.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question