Dir/vbDirectory bug (?)

Posted on 2014-12-19
Last Modified: 2014-12-19
Dear experts -
I have a simple line of code, to test if a directory exists - we've been using code like this for quite a while.
The problem is that it's telling me the directory DOES exist when it does NOT - apparently it is matching on a FILE within that directory that matches the criteria. it should only be looking to see if a SUBFOLDER exists.

Using pseudo-code, we are simply doing the following:
Dim strfolder As String
strfolder = Dir(STARTOFTHEFOLDERNAME & "*", vbDirectory)

The issue is that there is no subfolder that begins with STARTOFTHEFOLDERNAME, but rather a FILE that does begin with that string.

Any thoughts?

Question by:terpsichore
  • 3
  • 2
  • 2
  • +1
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 40508968
It's not a bug; wildcard operations are only allowed with files.   When you use the vbDirectory attribute, you must test for a specific name.

 Part of where the understanding comes in is that directories are nothing more than a special file entry.

LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40508971
post the original codes...

test this codes (place them in a regular module)

Function FolderFileExists(strFullPath As String) As Boolean

    If Dir(strFullPath, vbDirectory) <> vbNullString Then FolderFileExists = True
End Function


Sub TestFolderExists()
    If FolderFileExists("C:\Temp") Then
        MsgBox "Folder exists!"
        MsgBox "Folder does not exists!"
    End If
End Sub

Sub TestFileExists()
    If FolderFileExists("C:\Test\myFile.xls") Then
        MsgBox "File exists!"
        MsgBox "File does not exists!"
    End If
End Sub

Open in new window

if you are trying to find subfolder, try using FilescriptingObject

Author Comment

ID: 40508974
can you explain this:
if you are trying to find subfolder, try using FilescriptingObject

The issue is that users may change the name of a folder, so we can't test a given string...

Any other ideas are appreciated...

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40508975
the vbDirectory argument is not designed to only identify directories (folders), it is designed to return folders names as well as files.  If you want to test to determine whether a folder exists try:

if DIR(FullFolderName, vbDirectory) = "" Then
   msgbox "folder does not exist"
   msgbox "Folder exists"

Author Comment

ID: 40508984
But this is the problem - I don't have the 'fullfoldername' for the reason given above...

I suppose I can test to see whether there is a period in the name, since any file we have will have a file extension - is this an idea?
LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 250 total points
ID: 40508987
<The issue is that users may change the name of a folder, so we can't test a given string... >

you  must at least provide the root/main folder

Sub GetFolders(sPath As String)

Dim fso As Scripting.FileSystemObject
Dim srcFolder As Scripting.Folder, sbFolder As Scripting.Folder
Dim fileItem As Scripting.File

Set fso = New Scripting.FileSystemObject
Set srcFolder = fso.GetFolder(sPath)

For Each sbFolder In srcFolder.SubFolders
    Debug.Print sbFolder.Name

End Sub
LVL 57
ID: 40509004
<<But this is the problem - I don't have the 'fullfoldername' for the reason given above...>>

 What you need to do is scan a directory using the wildcard and build up an array.  Then do a test on each individual file name you have if it's a sub folder  or not.

Again, the vbDirectory attribute only works with a discrete file name.  You can't use it with Dir() to scan a folder for directories only


Author Closing Comment

ID: 40509007
Great, expert insights

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

778 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