Directory

Matt

Board Regular
Joined
Feb 16, 2002
Messages
212
In a macro that I have written, I create a new directory. Is it possible to first of all check that a directory with the same name already exists, if it does, not to bother to create the directory and if it does not go ahead and create the directory.

Hope someone can help

Thanks

Matt
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
try this code :
''''''''''''''''''''''''''
Function Test(YourDirectory As String) As Boolean
Test = False
TestName = Dir(TestPath, vbDirectory)
Do While TestName <> ""
If UCase$(YourDirectory) = UCase$(TestName) Then Test = True: Exit Do
TestName = Dir ' Get next directory
Loop
End Function
''''''''''''''''''''''''''''''''''''
where TestPath = "c:" or your path and YourDirectory is your folder for test.
Test is true if your folder already exists.
Good luck!
 
Upvote 0
Thanks for the code but I think I am being dumb as I cannot get it to run. I have tried to substitute the path and directory names as suggested, please could you resupply code if my path is C:/ and folder name is Matt.

Many thanks

Matt
 
Upvote 0
Hello matt,
sorry for my delay, I had a class.
copy these code in a module:
''''''''''''''''''''''''''''''''''''
Function Test_Folder(YourDirectory, TestPath) As Boolean
Test_Folder = False
GetFolder = Dir(TestPath, vbDirectory)
Do While GetFolder <> ""
If UCase$(YourDirectory) = UCase$(GetFolder) Then Test_Folder = True: Exit Do
GetFolder = Dir ' Get next directory
Loop
End Function

Sub Main_Test()

''' enter your path here
TestPath = "c:"
'''please enter your folder name
YourDirectory = "Matt"
If YourDirectory = "" Then Exit Sub
If Test_Folder(YourDirectory, TestPath) = True Then
MsgBox (YourDirectory & " already exists.")
Else
MsgBox (YourDirectory & " does not exist.")
End If

End Sub
''''''''''''''''''''''''''''''''''
now run Main_Test.
Good luck!
Hamid
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,027
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top