2015-01-19 12:31
浏览 349


Say we have two paths:

c:\foo\bar\baz and c:\foo\bar

Is there any package/method that will help me determine if one is a subdirectory of another? I am looking at a cross-platform option.

图片转代码服务由CSDN问答提供 功能建议


c:\ foo \ bar \ baz c:\ foo \ bar

是否有任何软件包/方法可以帮助我确定是否为以下内容的子目录: 另一个? 我正在查看跨平台选项。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dprnr5559 2015-01-19 12:41

    You could try and use path.filepath.Rel():

    func Rel(basepath, targpath string) (string, error)

    Rel returns a relative path that is lexically equivalent to targpath when joined to basepath with an intervening separator.
    That is, Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself

    That means Rel("c:\foo\bar", "c:\foo\bar\baz") should be baz, meaning a subpath completely included in c:\foo\bar\baz, and without any '../'.
    The same would apply for unix paths.

    That would make c:\foo\bar\baz a subdirectory of c:\foo\bar.

    解决 无用
    打赏 举报
  • duankan6894 2019-06-25 00:25

    You can use the function path.filepath.Match()

    Match reports whether name matches the shell file name pattern.

    For example:

    pattern := "C:\foo\bar" + string(filepath.Separator) + "*"
    matched, err := filepath.Match(pattern, "C:\foo\bar\baz")

    Where matched should be true.

    解决 无用
    打赏 举报

相关推荐 更多相似问题