Skip to content

Instantly share code, notes, and snippets.

@arehmandev
Created September 8, 2017 15:27
Show Gist options
  • Save arehmandev/20eaa4a4c73d14ed215fb18a9d759854 to your computer and use it in GitHub Desktop.
Save arehmandev/20eaa4a4c73d14ed215fb18a9d759854 to your computer and use it in GitHub Desktop.
Finding files in a directory using go - absolute filepath
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
@arehmandev
Copy link
Author

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
)

var (
	filepattern = "dir1"
	searchPath  = "./searchfolder"
	searchtype  = "folder" // change to 'file' for just files or 'all' for both files and folders
)

func main() {

	fmt.Println(search(filepattern, searchPath, searchtype))
}

func search(pattern, pathforsearching, filetype string) []string {

	var files []string

	if filetype == "all" {
		filepath.Walk(pathforsearching, func(path string, f os.FileInfo, _ error) error {
			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
}

@arehmandev
Copy link
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