Creating a directory  folder

latzo2 used Ask the Experts™
I have the folloing code to creat a dir folder to store a log file, however when I compile it, it does not creat the directory folders.  How can i get it to do that.

I have tried each of the following, the only way it works is if my executable is already in the C:\TCT folder.

MkDir "C:\TCT\Log"
MkDir CurDir & "C:\TCT\Log"
MkDir App.Path & "C:\TCT\log"

Dim iFileNumber As Integer
Dim strOutputFilename As String
MkDir "C:\TCT\Log"
'MkDir CurDir & "C:\TCT\Log"
'MkDir App.Path & "C:\TCT\log"

'MkDir "C:\TCT\log"

strOutputFilename = "C:\TCT\log\SystemLog.txt"

iFileNumber = FreeFile()

Open strOutputFilename For Output As #iFileNumber
 Print #iFileNumber, " User Machine Information "
 Print #iFileNumber, ""


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

try this:


nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

Public Sub CreateNewDirectory(NewDirectory As String)
    Dim sDirTest As String
    Dim bSuccess As Boolean
    Dim sPath As String
    Dim iCounter As Integer
    Dim sTempDir As String
    iFlag = 0
    sPath = NewDirectory
    If Right(sPath, Len(sPath)) <> "\" Then
        sPath = sPath & "\"
    End If
    iCounter = 1
    Do Until InStr(iCounter, sPath, "\") = 0
        iCounter = InStr(iCounter, sPath, "\")
        sTempDir = Left(sPath, iCounter)
        sDirTest = Dir(sTempDir)
        iCounter = iCounter + 1
        'create directory
        SecAttrib.lpSecurityDescriptor = &O0
        SecAttrib.bInheritHandle = False
        SecAttrib.nLength = Len(SecAttrib)
        bSuccess = CreateDirectory(sTempDir, SecAttrib)
End Sub

Private Sub Command1_Click()
Call CreateNewDirectory("C:\TCT\Log")
End Sub
I wouldn't start to bother with WIN API here, the problem here is only that

MkDir "C:\TCT\Log"

tryes to create Log into TCT folder that does not exist yet and is stupid enough not to crate it!

Just break it in two parts;

MkDir "C:\TCT"
MkDir "C:\TCT\Log"


Sub CreateFolder(ByVal FullPath As String)
    Dim saPart() As String
    Dim sPath As String
    Dim I As Long
    saPart = Split(FullPath, "\")
    For I = 0 To UBound(saPart)
        If I = 0 Then
            sPath = saPart(I)
            sPath = sPath & "\" & saPart(I)
        End If
        If Dir(sPath, vbDirectory) = "" Then
            MkDir sPath
        End If
End Sub

    CreateFolder "C:\TCT\Log"
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

What about this:
MkDir App.Path & "\log"


 you cold use the FilescriptingObject to create a directory.
  before you could use the below code you should first add a reference to the "Microsoft Scripting Runtime" on your project then:

 Actually, this Function checks if a folder exist, if it exist it creates it, if not it does nothing.

Function CheckFolder(strpath As String, strFolder As String)
'function to check if a folder exist
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
If fso.FolderExists(strpath & "\" & strFolder) = False Then
    fso.CreateFolder strpath & "\" & strFolder
End If
CheckFolder = strpath & "\" & strFolder & "\"
End Function

to call the function use
checkfolder "C:\TCT", "log"




if i do

MkDir App.Path & "\log"

how do i get the log file to be written within that dir

  you mean log is a file not a directory?
if you just want write file, sample:
    Dim iFileNumber As Integer
    iFileNumber = FreeFile()
    Open "C:\TCT\log.txt" For Output As #iFileNumber
    Print #iFileNumber, "1st line"
    Print #iFileNumber, "2nd line"
    Close #iFileNumber
if you not sure path "C:\TCT" exists, call CreateFolder("C:\TCT") before
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
strOutputFilename = App.Path & "\log\SystemLog.txt"
iFileNumber = FreeFile()
Open strOutputFilename For Output As #iFileNumber


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial