RosettaCodeData/Task/Empty-directory/FreeBASIC/empty-directory.basic

44 lines
1.1 KiB
Plaintext

' FB 1.05.0 Win64
#Include "dir.bi"
Function IsDirEmpty(dirPath As String) As Boolean
Err = 0
' check dirPath is a valid directory
Dim As String fileName = Dir(dirPath, fbDirectory)
If Len(fileName) = 0 Then
Err = 1000 ' dirPath is not a valid path
Return False
End If
' now check if there are any files/subdirectories in it other than . and ..
Dim fileSpec As String = dirPath + "\*.*"
Const attribMask = fbNormal Or fbHidden Or fbSystem Or fbDirectory
Dim outAttrib As UInteger
fileName = Dir(fileSpec, attribMask, outAttrib) ' get first file
Do
If fileName <> ".." AndAlso fileName <> "." Then
If Len(fileName) = 0 Then Return True
Exit Do
End If
fileName = Dir ' get next file
Loop
Return False
End Function
Dim outAttrib As UInteger
Dim dirPath As String = "c:\freebasic\docs" ' known to be empty
Dim empty As Boolean = IsDirEmpty(dirPath)
Dim e As Long = Err
If e = 1000 Then
Print "'"; dirPath; "' is not a valid directory"
End
End If
If empty Then
Print "'"; dirPath; "' is empty"
Else
Print "'"; dirPath; "' is not empty"
End If
Print
Print "Press any key to quit"
Sleep