Solved

Dir/vbDirectory bug (?)

Posted on 2014-12-19
8
166 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
  • 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 119

Expert Comment

by:Rey Obrero
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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 119

Accepted Solution

by:
Rey Obrero 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now