Created
July 27, 2015 06:54
-
-
Save thinca/a34136e2a961dd5108ae to your computer and use it in GitHub Desktop.
Unity の空のディレクトリの .meta ファイルを消すやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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