?
Solved

How to know if there is a directory and how to create it ?

Posted on 2011-10-27
5
Medium Priority
?
206 Views
Last Modified: 2012-08-13
How could I know if a directory exists and if not, how to create it ?

Regards
0
Comment
Question by:dimensionav
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 37042387
Pretty much the same as your other answer:

If Dir("C:\Test", vbDirectory) <> "" Then
  Msgbox "Folder exists"
Else
  MkDir "C:\Test"
End If 

Open in new window

0
 
LVL 14

Assisted Solution

by:VBClassicGuy
VBClassicGuy earned 1000 total points
ID: 37044375
With all due respect to kaufmed, that won't work. It has caught me several times. If a file exists with the same name, it will falsely say the folder exists. You need to use this:

If Dir("C:\Test", vbDirectory) <> "" Then
   If (GetAttr("C:\Test") And 16) = 16 Then
      MsgBox "Folder exists"
   Else
      MsgBox "File exists with same name"
   End If
Else
  MkDir "C:\Test"
End If
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37044505
With all due respect to kaufmed, that won't work
No worries. It's been a while since I had to do that in VB6. Now that you posted it, I recall having to check the attributes on the file also. Now I remember why I was so glad to move on from VB6  ; )
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 37044602
Glad you understand and I didn't step on any toes. Ya know, I think it's a bug in VB6...if you ask for a directory with the Dir$ command, you should only get directories. But if a file or directory exists with that name, you'll get it returned. You should not have to also check the file's attribute.

Like you say, .NET behaves much nicer :-)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37044626
The first time I saw an article describing that process I thought to myself, "I thought programming was supposed to make things easier. Why does it take XX lines to answer the question, 'Does this directory exist?'."

= D
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

862 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