How to resolve the algorithm Empty directory step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Empty directory step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Starting with a path to some directory, determine whether the directory is empty. An empty directory contains no files nor subdirectories. With Unix or Windows systems, every directory contains an entry for “.” and almost every directory contains “..” (except for a root directory); an empty directory contains no other entries.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Empty directory step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' 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

  

You may also check:How to resolve the algorithm Multiple distinct objects step by step in the C# programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the F# programming language
You may also check:How to resolve the algorithm Combinations step by step in the OCaml programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Java programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Rust programming language