Add IsDirEmpty

This commit is contained in:
Jae Kwon 2017-01-27 20:37:04 -08:00
parent 70e694ee76
commit 339e135776
1 changed files with 15 additions and 0 deletions

15
os.go
View File

@ -3,6 +3,7 @@ package common
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
@ -44,6 +45,20 @@ func EnsureDir(dir string, mode os.FileMode) error {
return nil
}
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return true, err //folder is non-existent
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
}
func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)