Create Directory

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Create Directory

Post by jstevens »

Hans,

I believe you provided me code at one time to create the entire folder path ie "C:\Temp\Hans\CleverClogs\MyFolder"

I cannot find it so if you recall, would you kindly provide the link or solution.

Thanks,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create Directory

Post by HansV »

I once posted such code in Woody's Lounge, but it has been hopelessly mangled there since. Here is the working version:

Code: Select all

Sub CheckFolder(strPath As String)
  Dim fso As Object
  Dim intPos As Integer
  Dim intLen As Integer
  intLen = Len(strPath)
  Set fso = CreateObject("Scripting.FileSystemObject")
  If Left(strPath, 2) = "\\" Then
    ' UNC path - skip server and share
    intPos = InStr(3, strPath, "\")
    intPos = InStr(intPos + 1, strPath, "\")
  Else
    ' drive letter - skip C: part
    intPos = 3
  End If
  Do
    intPos = InStr(intPos + 1, strPath, "\")
    If intPos = 0 Then
      intPos = intLen + 1
    End If
    If fso.FolderExists(Left(strPath, intPos - 1)) = False Then
      fso.CreateFolder Left(strPath, intPos - 1)
    End If
  Loop Until intPos = intLen + 1
  Set fso = Nothing
End Sub
You can call it with a path with a driveletter:

CheckFolder "C:\Temp\Hans\CleverClogs\MyFolder"

or with an UNC path:

CheckFolder "\\OurServer\Business\Templates\Excel\2010"
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Create Directory

Post by jstevens »

Hans,

I looked at the other Lounge but could not locate the code...boy have things changed.

Regards,
John
Regards,
John