JLRodgers
08-04-2005 at 06:24 AM
|
Re: check if FOLDER exist and if its EMPTY OR NOT?
IF this is put in a class module (or just a form or module...)
The public functions can see if a folder exists, and you could then check for files inside of it with this (like using *.* for the filename)
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Private Const MAX_PATH = 260
Private Const MAXDWORD = &HFFFF
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
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 Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Function CreateDir(ByVal PathName As String) As Boolean
Dim Security As SECURITY_ATTRIBUTES
Dim lRet As Long
'Create a directory
lRet = CreateDirectory(PathName, Security)
'If CreateDirectory returns 0, the function has failed
CreateDir = IIf(lRet <> 0, True, False)
End Function
Private Function StripNulls(ByVal OriginalStr As String) As String
On Error GoTo StripNulls_Err
' make sure we've got a string, strip the nulls if we do
If LenB(OriginalStr) Then
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
End If
StripNulls = OriginalStr
Exit Function
StripNulls_Err:
End Function
Private Function FindFileDir(ByVal Path As String, ByVal FileName As String, Optional ByVal IsDirectory As Boolean = False) As Boolean
Dim DirName As String ' SubDirectory Name
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
Dim fRet As Boolean
Dim sTmp As String
If Right(Path, 1) <> "\" Then Path = Path & "\"
Cont = True
fRet = False
sTmp = Replace(Path & FileName, "\\", "\")
hSearch = FindFirstFile(sTmp, WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' We've got a file, or directory
If LCase(DirName) = LCase(FileName) Then
If Not IsDirectory Then
fRet = True
ElseIf IsDirectory And (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
fRet = True
End If
End If
End If
Cont = FindNextFile(hSearch, WFD)
Loop While Cont Or Not fRet
Cont = FindClose(hSearch)
End If
FindFileDir = fRet
End Function
Public Function FileExists(ByVal FileName As String, ByVal Path As String) As Boolean
On Error GoTo FileExists_Err
FileExists = FindFileDir(Path, FileName, False)
Exit Function
FileExists_Err:
FileExists = False
End Function
Public Function DirectoryExists(ByVal DirectoryName As String, ByVal Path As String, Optional ByVal CreateDirectoryIfNotExist As Boolean = False) As Boolean
On Error GoTo DirectoryExists_Err
Dim fRet As Boolean
If FindFileDir(Path, DirectoryName, True) Then
' Directory already exists
fRet = True
Else
' Directory Doesn't exist
If CreateDirectoryIfNotExist Then
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
fRet = CreateDir(Path & DirectoryName)
Else
fRet = False
End If
End If
DirectoryExists = fRet
Exit Function
DirectoryExists_Err:
DirectoryExists = False
End Function
|
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|