Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check if a DIRECTORY exists, and if it doesn't, create it? 1

Status
Not open for further replies.

VBAguy22

IS-IT--Management
Aug 5, 2003
180
0
0
CA
Hello,
I have a program that has to copy some files from the LAN drive (R:\) to the local C:\ drive. Say, I want to copy all the files to C:\MyFiles\. Now, since many people will be using it, some might not have C:\MyFiles\ on their C:\ drive. How do I check if C:\MyFiles\ exists, and if it doesn't, how do I create it?

Thanks
 
VBAGuy,

Use this code I implemented in one of my programs, change it to suit your needs.

[tt]
Private Sub cmdCheckFolder_Click()
'Check if folder is there
If Len(Dir("c:\myfiles", vbDirectory)) = 0 Then
Dim Response As Integer
Dim sDir As String
'Ask user if they want to create folder
Response = MsgBox("Directory Does Not Exist, Create it?" _
, vbYesNo + vbQuestion, "Create Folder")
If Response = vbYes Then
sDir = "C:\MyFiles"
'Create Folder
MkDir sDir
End If
Else
'Code if Folder Exists
MsgBox "Folder Exists"
End If
End Sub
[/tt]

Hope this helps.


jgjge3.gif


[tt]"Do not put off until tomorrow what you cannot put off until the day after tomorrow just as well." - Mark Twain[/tt]
 
If you want to make this transparent to the user then:

On Error Resume Next
MkDir "C:\My Folder"



Experience is something you don't get until just after you need it.
 
Sorry, that should read:

On Error Resume Next
MkDir "C:\MyFiles

Experience is something you don't get until just after you need it.
 
Hate to be facetious BigAl but it should read

MkDir "C:\MyFiles" LOL ;)

Just to implement BigAl's Idea into My Code.

[tt]
Private Sub cmdCheckFolder_Click()
'Check if folder is there
If Len(Dir("c:\myfiles", vbDirectory)) = 0 Then
'Create Folder if folder doesn't exist
MkDir "C:\MyFiles"
Else
'Code if Folder Exists
MsgBox "Folder Exists"
End If
End Sub
[/tt]

Thanks Al,

Hope this helps VBAGuy!



jgjge3.gif


[tt]"Do not put off until tomorrow what you cannot put off until the day after tomorrow just as well." - Mark Twain[/tt]
 
Now I correct myself!!!

Sorry Al, didn't catch on to the errorhandler.

[tt]
On Error Resume Next
MkDir "C:\MyFiles"
[/tt]

This attempts to create the folder, and if it already exists, the errorhandler simply carries on with the code.

Now... (He Asks Himself While Hitting Himself On The Head)
Why Didn't I think of that???

Well Done Al,



jgjge3.gif


[tt]"Do not put off until tomorrow what you cannot put off until the day after tomorrow just as well." - Mark Twain[/tt]
 
>Jag, thanks for adding the second quote. With the error handler the damn folder would never have got created!!!

>Why didn't I think of that???


Let's just say 'age = experience' (I have a lot of experience) [pipe]


Experience is something you don't get until just after you need it.
 
Correction, if you fail to type the second quote VB puts the second quote in automatically anyway.

Experience is something you don't get until just after you need it.
 
Logically,

If Age = Experience then Experience = Age

From your threads posted, i'd say you gotta be at least 652yrs! lol



jgjge3.gif


[tt]"Do not put off until tomorrow what you cannot put off until the day after tomorrow just as well." - Mark Twain[/tt]
 
Close!

Experience is something you don't get until just after you need it.
 
I've found that you can only create a single directory from one that already exists. For example,

You want to create the directory C:\TEMP\AAAA and C:\TEMP does not exist already. You have to build the directory in two stages,

MKDIR "C:\TEMP"
MKDIR "C:\TEMP\AAAA"



 
No you dont...

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long

MakeSureDirectoryPathExists "c:\a\b\c\d\e\f\g\"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top