在golang中查找根目录中的特定文件?

huangapple go评论91阅读模式
英文:

Find particular file in a root directory in golang?

问题

我有一个代码,它可以递归地在包含子目录的根目录中查找作为输入参数传递的特定文件,并返回该文件的完整路径。

func findFile(root string, fileName string) ([]string, error) {
    var files []string
    err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
        if !d.IsDir() && d.Name() == fileName {
            files = append(files, path)
        }
        return nil
    })
    return files, err
}

这是我如何使用它的方式:

findFile("/home", "abc.json")

它可以正常工作,但现在我需要做更多的事情。而不是传递完整的文件名,我还应该能够传递类似于 prefix-*.json 的内容,这意味着找到所有以 prefix- 开头并以 .json 结尾的文件。有没有办法修改上述方法以适应这两种情况?如果没有,有什么更好的方法可以实现这个功能?

我希望能够执行以下操作,并返回匹配的所有文件的路径:

findFile("/home", "abc.json")
findFile("/home", "prefix-*.json")

更新

我使用以下代码使其工作:

func findFile(root string, fileName string) ([]string, error) {
    var files []string
    err := filepath.WalkDir(root, func(pathh string, d fs.DirEntry, err error) error {
        if !d.IsDir() {
            if ok, err := path.Match(fileName, d.Name()); ok && err == nil {
                files = append(files, pathh)
            }
        }
        return nil
    })
    return files, err
}

如果看起来没问题,请告诉我。还有在上述代码中是否有任何可以简化的地方?我有两个单独的 if 条件,它们可以合并成一个 if 条件吗?

英文:

I have a below code which finds a particular file passed as an input parameter recursively in a root directory which contains sub directories too and then it returns the full path of that file.

func findFile(root string, fileName string) ([]string, error) {
	var files []string
	err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
		if !d.IsDir() && d.Name() == fileName {
			files = append(files, path)
		}
		return nil
	})
	return files, err
}

This is how I use it -

findFile("/home", "abc.json")

It works fine but now I need to do one more thing. Instead of passing full complete fileName, I should also be able to pass something like this prefix-*.json which means find me all the files which starts with prefix- and ends with .json file type. Is there any way by which above method can be modified to work with both the cases? If not then what is the better way to do this?

I should be able to do below things and it will return me path of all those files matched.

findFile("/home", "abc.json")
findFile("/home", "prefix-*.json")

Update

I got it working with below code -

func findFile(root string, fileName string) ([]string, error) {
	var files []string
	err := filepath.WalkDir(root, func(pathh string, d fs.DirEntry, err error) error {
		if !d.IsDir() {
			if ok, err := path.Match(fileName, d.Name()); ok && err == nil {
				files = append(files, pathh)
			}
		}
		return nil
	})
	return files, err
}

Let me know if it looks good. Also anything I can simplify in my above code as I got two separate if conditions one by one. Can they be combined in one if condition?

答案1

得分: 3

我得到了两个单独的if条件,它们可以合并成一个if条件吗?

不可以:保持这些条件分开更清晰。

而且,由于ryanuber/go-glob是用来比较两个任意字符串的,所以在你的情况下,path.Match(fileName, d.Name())就足够了,因为你正在比较路径。

英文:

> as I got two separate if conditions one by one. Can they be combined in one if condition?

No: keeping those conditions separate is clearer.

And since ryanuber/go-glob is meant to compare two arbitrary strings, path.Match(fileName, d.Name()) is enough in your case, since you are comparing paths.

huangapple
  • 本文由 发表于 2021年12月31日 15:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/70539832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定