Work on media-parsing

This commit is contained in:
2017-09-16 18:03:32 +02:00
parent 1ed0b0b201
commit 73cc5d7f55
3 changed files with 129 additions and 0 deletions

35
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
@ -17,9 +18,43 @@ func main() {
fallthrough
case "rs":
suffix := args[1]
removeSuffix(suffix)
break
default:
fmt.Println("Invalid action specified")
return
}
}
func removeSuffix(suffix string) {
filepath.Walk(".", removeSuffixFunc(suffix))
}
func removeSuffixFunc(suffix string) filepath.WalkFunc {
return func(p string, f os.FileInfo, err error) error {
filename := f.Name()
if filename == "." || filename == ".." {
return nil
}
element := newMediaElement(p)
fmt.Println(element.String())
dir := filepath.Dir(p)
ext := filepath.Ext(filename)
filenameNoExt := trimSuffix(filename, ext)
filenameNoSuff := trimSuffix(filenameNoExt, suffix)
if filenameNoSuff != filenameNoExt {
newFilename := filenameNoSuff + ext
err := os.Rename(filepath.Join(dir, filename), filepath.Join(dir, newFilename))
if err != nil {
fmt.Printf("Could not rename %s: %v\n", filename, err)
} else {
fmt.Println("Renamed " + filename + " to " + newFilename)
}
}
return nil
}
}