Creating a Folder in C:\ from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to verify if a particular path and file exists in my c: drive and
if it doesn't create a folder in which to put it. For this argument Path is
C:\strA\strB\StrC and file name is strD.

Thank you very much in advance
 
if dir$("c:\a\b\c.d")="" then
' file c:\a\b\c.d does not exist.
else
' it does exist.
endif

' create folder c:\a\b.
mkdir "c:\a\b"

A slight complication, is that if the file c:\b\a\c.d does NOT exist,
this does not tell you anything about the two folders c:\a\b, and c:\a.
Those two folders might both exist, or just the first (but not the
second), or neither exists. And if you try to create a folder that
already exists, that will fail with a runtime error. So you might need
some error trapping: see the 'On Error' statement.

HTH,
TC [MVP Access]
 
My approach is a bit different than some others. I like to just go ahead and
build the folders and deal with the error if it arises:

Sub DoIt()
On Error GoTo Error_Handler
MkDir "C:\FolderA"
MkDir "C:\Folder A\FolderB"
MkDir "C:\Folder A\FolderB\FolderC"
Exit_Here:
Exit Sub
Error_Handler:
Resume Exit_Here
End Sub

If you are indeed using string variables for folder names, your last line
would look like:

MkDir "C:\" & strA & "\" & strB & "\" & strC
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
My only concern with that, would be that you don't check why the error
occurred. Perhaps the folder didn't exist, but you couldn't create it
because of a permissions problem. In that case, the code should fail
with a trappable runtime error IMHO.

I assumed that dir$() could check for folders, but apparently not :-(

TC [MVP Access]
 
TC said:
My only concern with that, would be that you don't check why the error
occurred. Perhaps the folder didn't exist, but you couldn't create it
because of a permissions problem. In that case, the code should fail
with a trappable runtime error IMHO.

I agree with you. That's why I usually use the pants-and-suspenders approach
of trying to add the folders, and then double-check that the target exists
before I try using it. For instance, I'll have a function that returns the
path of the folder to use, but I'll check that the path exists before trying
to use it.
I assumed that dir$() could check for folders, but apparently not :-(

It can. You just need to pass the correct parameter. I've got a folder named
Local on my C: drive. Here's the results of running Dir with different
configurations:

?Dir("C:\Local")

?Len(Dir("C:\Local"))
0
?Dir("C:\Local", vbDirectory)
Local

?Dir("C:\Local\")

?Len(Dir("C:\Local\"))
0
?Dir("C:\Local\", vbDirectory)
..

(in case it's not obvious, that's a period returned by the last call)
 
TC, Ofer & Doug,

Thanks guys. Most helpful.

Ofer said:
Check this link for creation of more then one folder

http://vbnet.mvps.org/index.html?code/file/nested.htm

--
\\// Live Long and Prosper \\//


TC said:
if dir$("c:\a\b\c.d")="" then
' file c:\a\b\c.d does not exist.
else
' it does exist.
endif

' create folder c:\a\b.
mkdir "c:\a\b"

A slight complication, is that if the file c:\b\a\c.d does NOT exist,
this does not tell you anything about the two folders c:\a\b, and c:\a.
Those two folders might both exist, or just the first (but not the
second), or neither exists. And if you try to create a folder that
already exists, that will fail with a runtime error. So you might need
some error trapping: see the 'On Error' statement.

HTH,
TC [MVP Access]
 
Back
Top