'start declarations for FileExists
Private Const INVALID_HANDLE_VALUE = -1
Public Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
' end declarations for FileExists
Public Function SaveTextToFile(FileFullPath As String, _
sText As String, Optional Overwrite As Boolean = True) As Boolean
'Purpose: Save Text to a file
'Parameters:
'-- FileFullPath - Directory/FileName to save file to
'-- sText - Text to write to file
'-- Overwrite (optional): If true, if the file exists, it
'is overwritten. If false,
'contents are appended to file
'if the file exists
'Returns: True if successful, false otherwise
'Example:
'SaveTextToFile "C:\My Documents\MyFile.txt", "Hello There"
On Error GoTo ErrorHandler
'check if dir exists, if not, make it
Dim dirend As Long
Dim thepathname As String
dirend = InStrRev(FileFullPath, "\")
thepathname = Left$(FileFullPath, dirend - 1)
If DirExists(thepathname) = False Then
MkDir thepathname
End If
Dim iFileNumber As Long
iFileNumber = FreeFile
If Overwrite Then
Open FileFullPath For Output As #iFileNumber
Else
Open FileFullPath For Append As #iFileNumber
End If
Print #iFileNumber, sText;
SaveTextToFile = True
ErrorHandler:
Close #iFileNumber
End Function
Public Function FileExists(sSource As String) As Boolean
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sSource, WFD)
FileExists = hFile <> INVALID_HANDLE_VALUE
Call FindClose(hFile)
End Function
Public Function DirExists(strDir As String) As Boolean
On Error GoTo PROC_ERR
DirExists = Len(Dir$(strDir & "\.", vbDirectory)) > 0
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , "DirExists"
Resume PROC_EXIT
End Function
Public Function ReadTxtFile(FileName$, Optional NoErrors As Boolean) As String
Dim tmpLine$
Dim fnum As Long
Dim flen As Long
Dim txt As String
On Error GoTo ErrorHandler
fnum = FreeFile
Open FileName$ For Input As fnum
txt = Input$(LOF(fnum), #fnum)
Close #fnum
ReadTxtFile = txt
Exit Function
ErrorHandler:
Close #fnum
If NoErrors = True Then Exit Function
Dim bFileExists As Boolean
bFileExists = FileExists(FileName$)
If bFileExists = False Then
MsgBox FileName$ & " not found.", vbApplicationModal + vbOKOnly, "ReadTxtFile"
Else
MsgBox FileName$ & " is not composed of text.", vbApplicationModal + vbOKOnly, "ReadTxtFile"
End If
End Function