Minor unexpected behavior in VBA DIR function

This is exceedingly minor.  However the following example brought two different results.

?Dir(“Q:\1 access\Fleet Mgmt\*.key”)
?Dir(“Q:\1 access\Fleet Mgmt\pd.key”)

Background.  For my “shrink wrap” application, the Granite Fleet Manager, I email the user a small encrypted license key file with a KEY extension.  This file contains information such as the organization name, the maximum number of units they are licensed for in the app and other similar data.

If a *.key file is found in the BE MDB folder then I pull in the data.  If not found then it’s a demo system.  (If more than one *.key file found I display a message and exit.  Should never happen of course.)

In this case I just renamed the file to have zzz after the “.key” extension just to test some logic.  And DIR still found the file.  Of course putting any character after the . and before “key” such as pd.zkeyzzz gave the expected result of nothing.

Now that function has been there for about nine years.  These days I would’ve used the FindFirstFile API call.  So is it worth changing?

I’m not going to fix it right away because I might introduce more errors in the code. Highly unlikely but possible. And the chances of anyone ever encountering this problem are very, very small.

I’ll put it in my To Do Tracking database but I may never get to it.  I did a quick search through the code and I do use DIR in a few other places.  But they look for all the files or a specific file name.  No cases using the wildcard *

P.S.  Yes, the Fleet Management web pages are rather sparse and ugly. I’m working on it.

Edited 2008-10-01 to update the Granite Fleet Manager URL

2 thoughts on “Minor unexpected behavior in VBA DIR function”

  1. Yes, Dir() get “*.abc” as “*.abc*”.
    I don’t know why but maybe this behavior comes from DOS dir command.
    Try dir in command prompt. You’ll see same result.
    One more thing.


    Try ‘dir *1.txt /x’ in command prompt.
    I think MS should be covered that in a help file.

  2. Good point. I’m sure that is the explanation. As to covering this in a help file? Well, this is a case that would be exceedingly obscure. But just guessing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>