Solved

Dir/vbDirectory bug (?)

Posted on 2014-12-19
8
167 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

914 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

21 Experts available now in Live!

Get 1:1 Help Now