Add DirectoryIsEmpty

This commit is contained in:
Ettore Di Giacinto
2021-02-09 16:51:03 +01:00
parent 505f07f056
commit fd80bb526e
2 changed files with 32 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
package helpers
import (
"io"
"io/ioutil"
"os"
"path/filepath"
@@ -82,6 +83,20 @@ func ListDir(dir string) ([]string, error) {
return content, err
}
// DirectoryIsEmpty Checks wether the directory is empty or not
func DirectoryIsEmpty(dir string) (bool, error) {
f, err := os.Open(dir)
if err != nil {
return false, err
}
defer f.Close()
if _, err = f.Readdirnames(1); err == io.EOF {
return true, nil
}
return false, nil
}
// Touch creates an empty file
func Touch(f string) error {
_, err := os.Stat(f)