PDA

View Full Version : Solved: CHECK for folder and create it



v_gyku
09-08-2005, 09:50 PM
:help I want to do the following thing...

Check if the folder ?RightAnswersTempWorkbooks? exists in the folder(take any).
i. If the folder does not exist, create it.
ii. If the folder exists, check for the existence of the folder ?RightAnswersTempWorkbooks<random number>? until a folder name is found that does not exist. Create the folder.

Jacob Hilderbrand
09-08-2005, 10:47 PM
Try this.

Option Explicit

Sub Macro1()

Dim Num As Long
Dim MaxNum As Long
Dim MinNum As Long
Dim Path As String

MinNum = 1
MaxNum = 1000
Path = "C:\RightAnswersTempWorkbooks"
On Error Resume Next
MkDir Path
Err.Clear
Randomize
Do
Num = Int((MaxNum - MinNum + 1) * Rnd + MinNum)
MkDir Path & "\RightAnswersTempWorkbooks" & Num
If Err = 0 Then
Exit Do
Else
Err.Clear
End If
Loop
On Error Goto 0
MsgBox "Folder Created: " & Path & "\RightAnswersTempWorkbooks" & Num

End Sub

Ken Puls
09-09-2005, 09:02 AM
Very pretty, Jake! :thumb