Categories: GoFrame 教程

GoFrame gfile-目录扫描

目录扫描

ScanDir

  • 说明:扫描指定目录,可扫描文件或目录,支持递归扫描。
  • 格式:
func ScanDir(path string, pattern string, recursive ...bool) ([]string, error)
  • 示例:
func ExampleScanDir() {
 // init
 var (
  fileName = "gflie_example.txt"
  tempDir  = gfile.TempDir("gfile_example_scan_dir")
  tempFile = gfile.Join(tempDir, fileName)

  tempSubDir  = gfile.Join(tempDir, "sub_dir")
  tempSubFile = gfile.Join(tempSubDir, fileName)
 )

 // write contents
 gfile.PutContents(tempFile, "goframe example content")
 gfile.PutContents(tempSubFile, "goframe example content")

 // scans directory recursively
 list, _ := gfile.ScanDir(tempDir, "*", true)
 for _, v := range list {
  fmt.Println(gfile.Basename(v))
 }

 // Output:
 // gflie_example.txt
 // sub_dir
 // gflie_example.txt
}

ScanDirFile

  • 说明:扫描指定目录的文件,支持递归扫描
  • 格式:
func ScanDirFile(path string, pattern string, recursive ...bool) ([]string, error)
  • 示例:
func ExampleScanDirFile() {
 // init
 var (
  fileName = "gflie_example.txt"
  tempDir  = gfile.TempDir("gfile_example_scan_dir_file")
  tempFile = gfile.Join(tempDir, fileName)

  tempSubDir  = gfile.Join(tempDir, "sub_dir")
  tempSubFile = gfile.Join(tempSubDir, fileName)
 )

 // write contents
 gfile.PutContents(tempFile, "goframe example content")
 gfile.PutContents(tempSubFile, "goframe example content")

 // scans directory recursively exclusive of directories
 list, _ := gfile.ScanDirFile(tempDir, "*.txt", true)
 for _, v := range list {
  fmt.Println(gfile.Basename(v))
 }

 // Output:
 // gflie_example.txt
 // gflie_example.txt
}

ScanDirFunc

  • 说明:扫描指定目录(自定义过滤方法),可扫描文件或目录,支持递归扫描
  • 格式:
func ScanDirFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error)
  • 示例:
func ExampleScanDirFunc() {
 // init
 var (
  fileName = "gflie_example.txt"
  tempDir  = gfile.TempDir("gfile_example_scan_dir_func")
  tempFile = gfile.Join(tempDir, fileName)

  tempSubDir  = gfile.Join(tempDir, "sub_dir")
  tempSubFile = gfile.Join(tempSubDir, fileName)
 )

 // write contents
 gfile.PutContents(tempFile, "goframe example content")
 gfile.PutContents(tempSubFile, "goframe example content")

 // scans directory recursively
 list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string {
  // ignores some files
  if gfile.Basename(path) == "gflie_example.txt" {
   return ""
  }
  return path
 })
 for _, v := range list {
  fmt.Println(gfile.Basename(v))
 }

 // Output:
 // sub_dir
}

ScanDirFileFunc

  • 说明:扫描指定目录的文件(自定义过滤方法),支持递归扫描。
  • 格式:
func ScanDirFileFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) 
  • 示例:
func ExampleScanDirFileFunc() {
 // init
 var (
  fileName = "gflie_example.txt"
  tempDir  = gfile.TempDir("gfile_example_scan_dir_file_func")
  tempFile = gfile.Join(tempDir, fileName)

  fileName1 = "gflie_example_ignores.txt"
  tempFile1 = gfile.Join(tempDir, fileName1)

  tempSubDir  = gfile.Join(tempDir, "sub_dir")
  tempSubFile = gfile.Join(tempSubDir, fileName)
 )

 // write contents
 gfile.PutContents(tempFile, "goframe example content")
 gfile.PutContents(tempFile1, "goframe example content")
 gfile.PutContents(tempSubFile, "goframe example content")

 // scans directory recursively exclusive of directories
 list, _ := gfile.ScanDirFileFunc(tempDir, "*.txt", true, func(path string) string {
  // ignores some files
  if gfile.Basename(path) == "gflie_example_ignores.txt" {
   return ""
  }
  return path
 })
 for _, v := range list {
  fmt.Println(gfile.Basename(v))
 }

 // Output:
 // gflie_example.txt
 // gflie_example.txt
}

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago