check if FOLDER exist and if its EMPTY OR NOT? [ http://www.andreavb.com/forum/viewtopic.php?TopicID=4866 ]


anig_29
08-04-2005 at 04:18 AM
check if FOLDER exist and if its EMPTY OR NOT?

Just wanna ask how to verify if folder exist (not FILE) but folder.. and how to determine if it is empty or not?  

If the folder is not empty then how to overwrite the existing folder contents?

Big thanks    




humberto
08-04-2005 at 05:49 AM
Re: check if FOLDER exist and if its EMPTY OR NOT?

You can use a filelistbox
File1.Path="Set directory path"
If File1.ListCount > 0 Then ' 0 not FILE



[Edited by humberto on 08-04-2005 at 06:52 AM GMT]




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)




anig_29
11-04-2005 at 02:41 AM
Re: check if FOLDER exist and if its EMPTY OR NOT?

hello.. thanks for the help.

but where exactly in the code does the finding/checking of the folder is? *am a bit confused *   - (without the overwriting/creating the directory)  

thanks agen  




JLRodgers
11-04-2005 at 06:27 AM
Re: check if FOLDER exist and if its EMPTY OR NOT?

the public function called "DirectoryExists"

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)




anig_29
11-04-2005 at 07:39 AM
how to determine if folder is empty or not

after verifying that the folder exists..

how to determine now, if its empty or not?

Thanks  




admin
11-04-2005 at 01:40 PM
Re: check if FOLDER exist and if its EMPTY OR NOT?

I've uploaded a little piece of code with two functions:

1) to check if a directory exists and...

2) to check if a directory is empty

you can find the code uploaded here:

http://www.andreavb.com/forum/database/index.php?action=showmod&mod=125

____________________________
AndreaVB




elizas
13-04-2010 at 12:04 PM
Re: check if FOLDER exist and if its EMPTY OR NOT?

At times we need to check if a folder is empty or not before performing an operation on it. For this, I have written a function which returns TRUE if any file or folder is found inside the folder and FALSE if the folder is empty.

/**
* Checking a folder is empty or not.
* @param string $folderName
* $folderName should be folder name or path
* @return TRUE/FALSE (If any file or folder found/Empty folder)
* By: Subhranil Dalal          subranild@mindfiresolutions.com  
* Mindfire Solutions           Date: 08-06-2009
*/folder