Folder Existence (1 Viewer)

aziz rasul

Active member
Local time
Today, 19:03
Joined
Jun 26, 2000
Messages
1,935
I have several DoCmd.TransferText statements which export's data to the C:\Text Files folder. If the "Text Files" folder does not exist, how can I check this using VBA and hence programatically create the folder.
 

prycejones

New member
Local time
Today, 19:03
Joined
Dec 14, 2001
Messages
3
Use the DIR command.
There are several constants you can use with this command, one of them is the VBDirectory.

Click help|contents|find and enter "dir function".

Hope this is what you want
 

BukHix

Registered User.
Local time
Today, 14:03
Joined
Feb 21, 2002
Messages
379
Create a new module and put this in it:

Function fIsFileDIR(stPath As String, Optional lngType As Long) As Integer
On Error Resume Next
fIsFileDIR = Len(Dir(stPath, lngType)) > 0
End Function


And then put this under a command button (as an example):

Dim strDir As String
Dim intDir As Integer

strDir = "c:\Text Files"
intDir = (fIsFileDIR(strDir, vbDirectory))

If intDir = 0 Then
MsgBox "I will create the directory now"
MkDir strDir
Else
MsgBox "Folder allready exists"
End If


That should get you started.



[This message has been edited by BukHix (edited 12-14-2001).]
 

ghudson

Registered User.
Local time
Today, 14:03
Joined
Jun 8, 2002
Messages
6,195
Same result with a lot less code...

Code:
    If Dir("C:\Text Files", vbDirectory) = "" Then
        MkDir ("C:\Text Files")
    Else
        'do nothing for the "C:\Text Files" directory already exists
        'MsgBox "C:\Text Files\ directory already exists"
    End If
HTH
 

DrChocolate

New member
Local time
Today, 14:03
Joined
Feb 21, 2012
Messages
7
Folder existence & creation with a variable

Dear ghudson, Your example is very clear except I fear that my brain is not wrapping itself well around

1) vbDirectory and
2) datestamping with a variable

despite searching and reading posts. Two quick questions:

1) What is the term vbDirectory and how do I use it? Would you post a very simple specific example showing how vbDirectory works?

2) I want a unique subdirectory name for each creation; may I substitute a variable for your example of MkDir ("C:\Text Files") such as

pathname = "SW_ & Format(now, yyyyddmmtttt"
MkDir (pathname)

which would translate into something such as

c:\SW_201209191320\

meaning: Sept 19, 2012 at 13:20.



Thanks for your time and expertise! Dr.C.
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:03
Joined
Aug 30, 2003
Messages
36,133
Hey Doc, in case ghudson isn't around (hasn't been around much), you can find info on 1 in VBA help on the Dir() function.

For 2, yes, you can build a string and use it in either function, as long as the string evaluates to a valid path. By that I mean your example of:

pathname = "SW_ & Format(now, yyyyddmmtttt"
MkDir (pathname)

didn't include the C:\, so wasn't a valid path.
 

boblarson

Smeghead
Local time
Today, 11:03
Joined
Jan 12, 2001
Messages
32,059
Re: Folder existence & creation with a variable

Two quick questions:

1) What is the term vbDirectory and how do I use it? Would you post a very simple specific example showing how vbDirectory works?
vbDirectory is a Visual Basic CONSTANT. Constants allow you to use an easy to remember name instead of having to type the value (the value of vbDirectory by the way is 16, which you can find out yourself on any constant by typing it into your VBA IMMEDIATE WINDOW like:
?vbDirectory

and then hit your ENTER key. It will then return the value like this:
?vbdirectory
16



2) I want a unique subdirectory name for each creation; may I substitute a variable for your example of MkDir ("C:\Text Files") such as

pathname = "SW_ & Format(now, yyyyddmmtttt"
MkDir (pathname)

which would translate into something such as

c:\SW_201209191320\

meaning: Sept 19, 2012 at 13:20.
Yes, you should be able to do it, but you don't use the parentheses. It would just be

MkDir pathname
 

Users who are viewing this thread

Top Bottom