Skip to content

Instantly share code, notes, and snippets.

@thinca
Created July 27, 2015 06:54
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 thinca/a34136e2a961dd5108ae to your computer and use it in GitHub Desktop.
Save thinca/a34136e2a961dd5108ae to your computer and use it in GitHub Desktop.
Unity の空のディレクトリの .meta ファイルを消すやつ
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err
}
func RemoveTail(str string, tail string) string {
return str[0 : len(str)-len(tail)]
}
func main() {
if len(os.Args) <= 1 {
fmt.Fprintln(os.Stderr, "Usage: remove-empty-dir-meta {dir}")
os.Exit(1)
}
root := os.Args[1]
err := filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".meta") {
dir := RemoveTail(path, ".meta")
ok, err := IsDirEmpty(dir)
if err == nil && ok {
os.Remove(dir)
os.Remove(path)
fmt.Println("Removed: " + path)
}
}
return nil
})
if err != nil {
fmt.Println(1, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment