Created
September 8, 2017 15:27
-
-
Save arehmandev/20eaa4a4c73d14ed215fb18a9d759854 to your computer and use it in GitHub Desktop.
Finding files in a directory using go - absolute filepath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "os" | |
| "path/filepath" | |
| "regexp" | |
| ) | |
| var ( | |
| fileExtension = ".properties" | |
| searchPath = "../" | |
| files []string | |
| ) | |
| func main() { | |
| fmt.Println(checkExt(fileExtension, searchPath)) | |
| } | |
| func checkExt(ext, pathforsearching string) []string { | |
| pathS := searchPath | |
| filepath.Walk(pathS, func(path string, f os.FileInfo, _ error) error { | |
| if !f.IsDir() { | |
| r, err := regexp.MatchString(ext, f.Name()) | |
| if err == nil && r { | |
| absolutefilepath, err := filepath.Abs(path) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| files = append(files, absolutefilepath) | |
| } | |
| } | |
| return nil | |
| }) | |
| return files | |
| } | |
| // This returns the absolute filepath to each file - use this one instead if you just want the filenames: https://gist.github.com/aln787/ea40d18cc33c7a983549 |
Author
arehmandev
commented
Sep 21, 2017
Author
Or using a method:
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
func main() {
myfirstfile := Filesearch{Pattern: "properties", Path: "./", Filetype: "all"}
filearray := myfirstfile.Searchv2()
fmt.Println(filearray)
}
// Filesearch - type for instantiating a search method
type Filesearch struct {
Pattern string
Path string
Filetype string
}
// Searchv2 - this is a function for searching a directory for a file/folder/both using the filesearch type
func (file *Filesearch) Searchv2() []string {
var files []string
pattern := file.Pattern
pathforsearching := file.Path
filetype := file.Filetype
if filetype == "all" {
filepath.Walk(pathforsearching, func(path string, f os.FileInfo, _ error) error {
// if !f.IsDir() {
r, err := regexp.MatchString(pattern, f.Name())
if err == nil && r {
absolutefilepath, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
files = append(files, absolutefilepath)
// }
}
return nil
})
}
if filetype == "file" {
filepath.Walk(pathforsearching, func(path string, f os.FileInfo, _ error) error {
if !f.IsDir() {
r, err := regexp.MatchString(pattern, f.Name())
if err == nil && r {
absolutefilepath, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
files = append(files, absolutefilepath)
}
}
return nil
})
}
if filetype == "folder" {
filepath.Walk(pathforsearching, func(path string, f os.FileInfo, _ error) error {
if f.IsDir() {
r, err := regexp.MatchString(pattern, f.Name())
if err == nil && r {
absolutefilepath, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
files = append(files, absolutefilepath)
}
}
return nil
})
}
return files
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment