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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a Folder

Status
Not open for further replies.

FoxProProgrammer

Programmer
Apr 26, 2002
967
US
Does anyone know how to check if a folder exists on a drive, and if not, create it? I found the CreateFolder Method in the Help, but the example is the pits and doesn't seem to apply to my situation. The Help for CreateFolder has the following syntax:

object.CreateFolder(foldername),

where object is a FileSystemObject and foldername is the string expression of the folder to create.

I looked up FileSystemObject and didn't see anything that applies to what I am trying to do. How do I define the object so I can create the folder? It shouldn't be this difficult to see if a folder exists, and create a folder!

Thanks for any guidance.

dz

 
To create a folder named "D" in the temporary folder:

Const TemporaryFolder = 2
Dim fs, d, s
Dim myTmpPath As String

Set fs = CreateObject("Scripting.FileSystemObject")
myTmpPath = fs.GetSpecialFolder(TemporaryFolder)

'create directory if it does not exist
If Dir(myTmpPath & "\D", vbDirectory) = "" Then Call MkDir(myTmpPath & "\D\")

ide
 
Thank you!

I didn't need the temporary folder, but figured out how to do what I needed with your help.

dz
 
I know you already figured it out, but here is a simple function to use. To use it, you simply pass it the name of the folder you want to verify:

If DoesFolderExist("C:\My Documents") Then ...

==========
Public Function DoesFolderExist(FolderName As String) As Boolean
Dim fso As New FileSystemObject
DoesFolderExist = fso.FolderExists(FolderName)
Set fso = Nothing
End Function
==========

Just remember to set the reference (in code window, go to Tools | References) to the Microsoft Scripting Runtime.

You can do anything to files and folders with the FileSystemObject, Move, Verify, Copy, etc. Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top