VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



CreateDirX

by Laurence Lemmon-Warde (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

This code will search the user's C:\ (or any other specified) drive for a given folder and if the folder is not found, it will call the CreateDirX function which in turn calls the API CreateDirectory function to create the specified folder. Once this is created, it will create a new notepad file within the folder and on each subsequent running of the application it will append info to this file. Great for logfile requirements!!

API Declarations
Public Declare Function CreateDirectory Lib "kernel32" Alias _
"CreateDirectoryA" (ByVal lpPathname As String, lpSecurityAttributes _
As SECURITY_ATTRIBUTES) As Long
'Insert into global module
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Variant
bInheritHandle As Boolean
End Type

Rate CreateDirX

'include a common dialog control on your form for this baby to work
Public Sub OpenLog()
Dim LogFile as integer 
On Error GoTo exit1
 OpenLog.Flags = cdlOFNHideReadOnly Or cdlOFNExplorer
 OpenLog.CancelError = True
 OpenLog.FileName = "C:\JetLog\JET_LOG.log"  ' or whatever name grabs you by                       ' the nads
 temp = OpenLog.FileName
 Ret = Len(Dir$(temp))
 LogFile = FreeFile
 ' Open the log file.
 Open temp For Binary Access Write As LogFile
 If Err Then
  Exit Sub
 Else
  ' Go to the end of the file so that new data can be appended.
  Seek LogFile, LOF(LogFile) + 1
 End If
 Exit Sub
exit1:  ' Executes if folder is not found
 MsgBox "Application will create new directory 'C:\JetLog' on your hard drive." & vbCrLf & "Replace message with your own text.", vbExclamation, "Message"
 CreateDirX ("C:\JetLog")  'pass the path name you want to create in              ' these brackets
 OpenLog_Click
End Sub
Private Function CreateDirX(lpPathname As String) As Long
 Dim FYL As Long
 Dim DirC As SECURITY_ATTRIBUTES
  FYL = CreateDirectory(lpPathname, DirC)
End Function

Download this snippet    Add to My Saved Code

CreateDirX Comments

No comments have been posted about CreateDirX. Why not be the first to post a comment about CreateDirX.

Post your comment

Subject:
Message:
0/1000 characters