gf/os/gfile/gfile_z_example_scan_test.go

134 lines
3.2 KiB
Go
Raw Normal View History

2021-11-17 14:14:19 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-11-05 14:30:50 +08:00
package gfile_test
import (
"fmt"
"github.com/gogf/gf/v2/os/gfile"
)
func ExampleScanDir() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_scan_dir")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
tempSubDir = gfile.Join(tempDir, "sub_dir")
tempSubFile = gfile.Join(tempSubDir, fileName)
)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// write contents
gfile.PutContents(tempFile, "goframe example content")
gfile.PutContents(tempSubFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// scans directory recursively
2021-11-13 23:42:03 +08:00
list, _ := gfile.ScanDir(tempDir, "*", true)
2021-11-05 14:30:50 +08:00
for _, v := range list {
fmt.Println(gfile.Basename(v))
}
// Output:
2021-11-11 13:01:32 +08:00
// gflie_example.txt
2021-11-05 14:30:50 +08:00
// sub_dir
2021-11-11 13:01:32 +08:00
// gflie_example.txt
2021-11-05 14:30:50 +08:00
}
func ExampleScanDirFile() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_scan_dir_file")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
tempSubDir = gfile.Join(tempDir, "sub_dir")
tempSubFile = gfile.Join(tempSubDir, fileName)
)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// write contents
gfile.PutContents(tempFile, "goframe example content")
gfile.PutContents(tempSubFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// scans directory recursively exclusive of directories
2021-11-17 19:50:03 +08:00
list, _ := gfile.ScanDirFile(tempDir, "*.txt", true)
2021-11-05 14:30:50 +08:00
for _, v := range list {
fmt.Println(gfile.Basename(v))
}
// Output:
2021-11-11 13:01:32 +08:00
// gflie_example.txt
// gflie_example.txt
2021-11-05 14:30:50 +08:00
}
func ExampleScanDirFunc() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_scan_dir_func")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
tempSubDir = gfile.Join(tempDir, "sub_dir")
tempSubFile = gfile.Join(tempSubDir, fileName)
)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// write contents
gfile.PutContents(tempFile, "goframe example content")
gfile.PutContents(tempSubFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// scans directory recursively
2021-11-13 23:42:03 +08:00
list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string {
2021-11-05 14:30:50 +08:00
// ignores some files
2021-11-11 13:01:32 +08:00
if gfile.Basename(path) == "gflie_example.txt" {
2021-11-05 14:30:50 +08:00
return ""
}
return path
})
for _, v := range list {
fmt.Println(gfile.Basename(v))
}
// Output:
// sub_dir
}
func ExampleScanDirFileFunc() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_scan_dir_file_func")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
fileName1 = "gflie_example_ignores.txt"
tempFile1 = gfile.Join(tempDir, fileName1)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
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")
2021-11-05 14:30:50 +08:00
// scans directory recursively exclusive of directories
2021-11-17 19:50:03 +08:00
list, _ := gfile.ScanDirFileFunc(tempDir, "*.txt", true, func(path string) string {
2021-11-05 14:30:50 +08:00
// ignores some files
2021-11-11 13:01:32 +08:00
if gfile.Basename(path) == "gflie_example_ignores.txt" {
2021-11-05 14:30:50 +08:00
return ""
}
return path
})
for _, v := range list {
fmt.Println(gfile.Basename(v))
}
// Output:
2021-11-11 13:01:32 +08:00
// gflie_example.txt
// gflie_example.txt
2021-11-05 14:30:50 +08:00
}