即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

golang rename patch

编程语言 hitxqh 9℃ 0评论
package main

import (
    "flag"
    "log"
    "os"
    "strings"
    "path/filepath"
)

var flagPath = flag.String("path", "", "文件夹")
var flagReplace = flag.String("replace", "", "要替换的内容")
var flagTo = flag.String("to", "", "替换成的内容")

func visit(path string, f os.FileInfo, err error) (e error) {
    dir := filepath.Dir(path)
    base := filepath.Base(path)

    newname := filepath.Join(dir, strings.Replace(base, *flagReplace, *flagTo, 1))
    log.Printf("mv \"%s\" \"%s\"\n", path, newname)
    os.Rename(path, newname)
    // }
    return
}


func init() {
    flag.Parse()
}

func main() {
    if *flagPath == "" && *flagReplace == "" {
        flag.Usage()
        return
    }
    filepath.Walk(*flagPath, visit)
}

转载请注明:CodingBlog » golang rename patch

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情