Solved

Dir/vbDirectory bug (?)

Posted on 2014-12-19
8
171 Views
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?

Thanks!
0
Comment
Question by:terpsichore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
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.

Jim.
0
 
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!"
    Else
        MsgBox "Folder does not exists!"
    End If
End Sub

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

Open in new window



if you are trying to find subfolder, try using FilescriptingObject
0
 

Author Comment

by:terpsichore
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...

Thanks,
0
Independent Software Vendors: 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!

 
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"
Else
   msgbox "Folder exists"
endif
0
 

Author Comment

by:terpsichore
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?
0
 
LVL 120

Accepted Solution

by:
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
   
Next

End Sub
0
 
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

Jim.
0
 

Author Closing Comment

by:terpsichore
ID: 40509007
Great, expert insights
0

Featured Post

Independent Software Vendors: 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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

742 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