Solved

Dir/vbDirectory bug (?)

Posted on 2014-12-19
8
173 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 58

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
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!

 
LVL 48

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 58
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

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.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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