ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If Folder Does Exit Create (https://www.excelbanter.com/excel-programming/400824-if-folder-does-exit-create.html)

Joe K.

If Folder Does Exit Create
 

I am searching for easy VBA script to create a folder (C:\LBA\Temp) if does
not exist.

Please help me with this task.

Thanks,

Ian[_4_]

If Folder Does Exit Create
 
Sub test()
Dim fs As Object
foldername = "c:\LBA\Temp"
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(foldername) Then
fs.CreateFolder (foldername)
End If
End Sub

This will only work if c:\LBA exists. If there's any chance it may be
missing you'll have to search for/create this folder first using the same
method.

Ian

"Joe K." <Joe wrote in message
...

I am searching for easy VBA script to create a folder (C:\LBA\Temp) if
does
not exist.

Please help me with this task.

Thanks,




Chip Pearson

If Folder Does Exit Create
 
Try something like

On Error Resume Next
MkDir "C:\Test123"
On Error GoTo 0

This will create "C:\Test123" if it does not exist. If it already exists,
the error is ignored.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Joe K." <Joe wrote in message
...

I am searching for easy VBA script to create a folder (C:\LBA\Temp) if
does
not exist.

Please help me with this task.

Thanks,



Rick Rothstein \(MVP - VB\)

If Folder Does Exit Create
 
I am searching for easy VBA script to create a folder (C:\LBA\Temp)
if does not exist.


Here is a routine I once posted in the compiled VB newsgroups, but it will
work in Excel's VBA...

Sub MakeDirectories(ByVal PathIn As String)
Dim X As Long
If Right$(PathIn, 1) < "\" Then PathIn = PathIn & "\"
X = InStr(1, PathIn, "\")
Do While X < 0
If Dir$(Left$(PathIn, X), vbDirectory) = "" Then
MkDir Left$(PathIn, X)
End If
X = InStr(X & 1, PathIn, "\")
Loop
End Sub

If any part of the path already exists, it will add the part that doesn't
exist to the part that already exists. You would use it from your own code
like this...

MakeDirectories "c:\LBA\Temp"

You can chain more than the two subdirectories shown in your example
request. For example...

MakeDirectories "c:\1stDir\2ndDir\3rdDir\4thDir"

Rick



All times are GMT +1. The time now is 06:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com