Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@priesdelly
Last active September 3, 2020 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priesdelly/e4f4b1c864e5d3ce3ca36e2cbbf979a6 to your computer and use it in GitHub Desktop.
Save priesdelly/e4f4b1c864e5d3ce3ca36e2cbbf979a6 to your computer and use it in GitHub Desktop.
List filename and write to text file
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
)
func main() {
var files []string
var rootDir []string
rootDir = append(rootDir, "~/Downloads")
rootDir = append(rootDir, "~/Picture")
for _, rDir := range rootDir {
err := filepath.Walk(rDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
// if !strings.HasSuffix(info.Name(), ".mkv") {
// return nil
// }
files = append(files, info.Name())
return nil
})
if err != nil {
panic(err)
}
}
sort.Strings(files)
f, err := os.Create("filename.txt")
if err != nil {
fmt.Println(err)
return
}
for _, file := range files {
// fmt.Println(file)
_, err := f.WriteString(file + "\n")
if err != nil {
fmt.Println(err)
f.Close()
return
}
}
err = f.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment