Documentation ¶
Overview ¶
Package dirsearch offers some useful functions for searching directories and returning useful information.
Index ¶
- func Count(name string, checks ...check.FileInfo) (int, []error)
- func CountRecurse(name string, checks ...check.FileInfo) (int, []error)
- func CountRecursePrune(name string, maxDepth int, dirChecks []check.FileInfo, ...) (int, []error)
- func Find(dirName string, checks ...check.FileInfo) (map[string]fs.FileInfo, []error)
- func FindRecurse(dirName string, checks ...check.FileInfo) (map[string]fs.FileInfo, []error)
- func FindRecursePrune(dirName string, maxDepth int, dirChecks []check.FileInfo, ...) (map[string]fs.FileInfo, []error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Count ¶
Count returns the number of entries in the directory that match the supplied checks (if any) and any errors detected
func CountRecurse ¶
CountRecurse returns the number of entries in the directory and any sub-directories that match the supplied checks (if any) and any errors detected
func CountRecursePrune ¶
func CountRecursePrune(name string, maxDepth int, dirChecks []check.FileInfo, checks ...check.FileInfo, ) (int, []error)
CountRecursePrune returns the number of entries in the directory and any sub-directories that match the supplied checks (if any) and any errors detected
func Find ¶
Find reads the directory and returns the FileInfo details for each entry for which all the checks return true. Any errors are also returned. The details are returned in a map of file names (including the directory prefix) to FileInfo
Example ¶
package main import ( "fmt" "github.com/nickwells/dirsearch.mod/v2/dirsearch" ) func main() { info, errs := dirsearch.Find("testdata/examples/dir1") if len(errs) != 0 { fmt.Println("Unexpected errors") for _, err := range errs { fmt.Println("\t", err) } return } for k, v := range info { sizeStr := "Non-Empty" if v.Size() == 0 { sizeStr = "Empty" } fmt.Println("file:", k, "=", sizeStr) } }
Output: file: testdata/examples/dir1/non-empty-file1 = Non-Empty file: testdata/examples/dir1/.hidden-subdir1 = Non-Empty file: testdata/examples/dir1/empty-file1 = Empty file: testdata/examples/dir1/.non-empty-hidden-file1 = Non-Empty file: testdata/examples/dir1/subdir1 = Non-Empty
Example (WithChecks) ¶
This demonstrates use of the Find function with checks supplied to return only non-empty regular files
package main import ( "fmt" "github.com/nickwells/check.mod/v2/check" "github.com/nickwells/dirsearch.mod/v2/dirsearch" ) func main() { info, errs := dirsearch.Find("testdata/examples/dir1", check.FileInfoSize(check.ValGT[int64](0)), check.FileInfoIsRegular) if len(errs) != 0 { fmt.Println("Unexpected errors") for _, err := range errs { fmt.Println("\t", err) } return } for k, v := range info { sizeStr := "Non-Empty" if v.Size() == 0 { sizeStr = "Empty" } fmt.Println("file:", k, "=", sizeStr) } }
Output: file: testdata/examples/dir1/non-empty-file1 = Non-Empty file: testdata/examples/dir1/.non-empty-hidden-file1 = Non-Empty
func FindRecurse ¶
FindRecurse reads the directory and returns the FileInfo details for each entry for which all the checks return true. Any errors are also returned. The details are returned in a map of file names (including the directory prefixes) to FileInfo. This differs from Find(...) in that any entries in the directory which are themselves directories (except for "." and "..") are descended into and the search continues in that sub-directory
Example (WithChecks) ¶
This demonstrates use of the FindRecurse function with checks supplied to return only non-empty regular files
package main import ( "fmt" "github.com/nickwells/check.mod/v2/check" "github.com/nickwells/dirsearch.mod/v2/dirsearch" ) func main() { info, errs := dirsearch.FindRecurse("testdata/examples/dir1", check.FileInfoSize(check.ValGT[int64](0)), check.FileInfoIsRegular) if len(errs) != 0 { fmt.Println("Unexpected errors") for _, err := range errs { fmt.Println("\t", err) } return } for k, v := range info { sizeStr := "Non-Empty" if v.Size() == 0 { sizeStr = "Empty" } fmt.Println("file:", k, "=", sizeStr) } }
Output: file: testdata/examples/dir1/non-empty-file1 = Non-Empty file: testdata/examples/dir1/subdir1/non-empty-file = Non-Empty file: testdata/examples/dir1/subdir1/subsubdir1/non-empty-file1 = Non-Empty file: testdata/examples/dir1/.hidden-subdir1/non-empty-file = Non-Empty file: testdata/examples/dir1/.non-empty-hidden-file1 = Non-Empty
func FindRecursePrune ¶
func FindRecursePrune(dirName string, maxDepth int, dirChecks []check.FileInfo, checks ...check.FileInfo, ) (map[string]fs.FileInfo, []error)
FindRecursePrune reads the directory and returns the FileInfo details for each entry for which all the checks return true. Any errors are also returned. The details are returned in a map of file names (including the directory prefixes) to FileInfo. This differs from FindRecurse(...) in that the entries in the directory which are themselves directories are checked to see that they are not too deep and that they pass the dirChecks and skipped if not. Pass a maxDepth < 0 to ignore the depth of descent down the directory tree.
Example (WithChecks) ¶
This demonstrates use of the FindRecursePrune function with checks supplied to return only non-empty regular files and a slice of directory checks provided to prevent descent into hidden directories (those with a name starting with a '.')
package main import ( "fmt" "os" "github.com/nickwells/check.mod/v2/check" "github.com/nickwells/dirsearch.mod/v2/dirsearch" ) func main() { info, errs := dirsearch.FindRecursePrune("testdata/examples/dir1", -1, []check.ValCk[os.FileInfo]{ check.FileInfoName( check.Not( check.StringHasPrefix[string]("."), "no leading '.'")), }, check.FileInfoSize(check.ValGT[int64](0)), check.FileInfoIsRegular) if len(errs) != 0 { fmt.Println("Unexpected errors") for _, err := range errs { fmt.Println("\t", err) } return } for k, v := range info { sizeStr := "Non-Empty" if v.Size() == 0 { sizeStr = "Empty" } fmt.Println("file:", k, "=", sizeStr) } }
Output: file: testdata/examples/dir1/non-empty-file1 = Non-Empty file: testdata/examples/dir1/.non-empty-hidden-file1 = Non-Empty file: testdata/examples/dir1/subdir1/non-empty-file = Non-Empty file: testdata/examples/dir1/subdir1/subsubdir1/non-empty-file1 = Non-Empty
Types ¶
This section is empty.