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

Check if folder exists and if not create 1

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
Is there an easy way to check and see if a specific folder already exists, and if it doesn't, create the folder?
 

Code:
If Dir(&quot;F:\dirname\nul&quot;, vbNormal) <> &quot;&quot; Then
    MsgBox &quot;dir exists&quot;
Else
   MkDir &quot;F:\Dirname&quot;
End If

John
 
Hi John,

Here's the macro I set up in a test template:
Code:
Sub AutoNew()

Dim strFolder As String
Dim strFilename As String

strFolder = &quot;C:\Temp\Testing Folder&quot;
strFilename = &quot;File &quot;

' Determine whether folder exists in C:\Temp
If Dir(strFolder, vbNormal) <> &quot;&quot; Then
    ' Folder already exists
Else
    ' Folder needs to be created
   MkDir strFolder
End If

' Save new file. If file already exists, add a number at the end to protect
' original file
If Dir(strFolder & &quot;\&quot; & strFilename & &quot;.doc&quot;) = &quot;&quot; Then
    ActiveDocument.SaveAs (strFolder & &quot;\&quot; & strFilename & &quot;.doc&quot;)
Else
    N = 1
    Do While Dir(strFolder & &quot;\&quot; & strFilename & N & &quot;.doc&quot;) <> &quot;&quot;
        N = N + 1
    Loop
    ActiveDocument.SaveAs strFolder & &quot;\&quot; & strFilename & N & &quot;.doc&quot;
End If

End Sub

The first time through it works fine, creates the new folder as it did not already exist, and saves a new file to it called &quot;File &quot;.

The second time I run the template, instead of creating a new file called &quot;File 1&quot; in the previously created folder, I get an error on the following line of code:

[code\MkDir strFolder[/code]

Any suggestions?

Thanks!

Cheryl
 
I would check and see if there is a file called &quot;Testing Folder&quot; in the C:\Temp directory.

This is because the Dir() function will return true if there is either a file or directory (depending upon the alue of the second parameter). With your current code, it will only check for the existence of a file and not a directory (the old name for a folder).
The \nul bit in my code is a variation on an old batch file trick to check for the existence of a directory (ie it is not just an ordinary file).
However, a more elegant way is to use the vbDirectory parameter to check for the existence of a directory of that name

I would make the following change to your code to check:

Code:
' Determine whether folder exists in C:\Temp
If Dir(strFolder, vbDirectory) = &quot;&quot; Then
   ' No it doesn't, so carry on...
   If Dir (strFolder, vbNormal) <> &quot;&quot; Then Kill strFolder
   ' delete any normal file with that name.
   ' now create the folder
   MkDir strFolder
End If

Of course, there is always the chance that your file is read only, hidden or system and thus the Kill will fail, but that is unlikely to be if it is in a temp directory that you are wanting to delete. I would check out the Attr function for resetting file attributes if this causes a problem.

Regards,

John
 
John,

Thank you very much! This now works great!

Cheryl
 
Using the code I gave you in the other thread...


' Determine whether folder exists in C:\Temp
Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If Not FSO.FolderExists(strFolder) Then
Set CreatedFolder = FSO.CreateFolder(strFolder)
End If


(P.S. Just FYI - try not to ask the same question in multiple threads, or at least, tell people that there is another similar thread. You may get different answers to the same question that other people don't know exist.)
 
I did send a message after I found a second thread was created. When I tried to send my original question, I got a message that it couldn't complete due to high volume. When I resent it, I found that it now appeared twice.

Sorry!
 
This is what I use... pass it a path and creates any of the elements the path needs without using FSO

Public Function createDirTree(ByVal path As String) As Boolean
'Creates the directory tree if it does not already exist
Dim x, newpath
path = path + &quot;\&quot;
For x = 1 To Len(path)
newpath = Left(path, x)
If Right(newpath, 1) = &quot;\&quot; Then
If Len(newpath) > 3 Then 'Basically skip the root of the drive
'check if folder exists
If Len(Dir(Left(path, x - 1), 16)) = 0 Then
'create directory
MkDir (newpath)
End If
End If
End If
Next
createDirTree = True
End Function

-Pete
Do you get a little guilty pleasure when a celebrity has a bad day?
Well then The Dead Pool is for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top