package main import "fmt" "io/ioutil" "strings" "os" "path/filepath" ) func main) { path := "." files, _ := ioutil.ReadDirpath) for index, f := range files { // 带扩展名的文件名 fullFilename := f.Name) fmt.Printlnindex) fmt.PrintlnfullFilename) //扩展名 fileExt := filepath.Extf.Name)) fmt.PrintlnfileExt) // 不带扩展名的文件名 filenameOnly := strings.TrimSuffixfullFilename, fileExt) fmt.PrintlnfilenameOnly) //将每个文件名后面加上1,扩展名不变 //os.Renamepath+"\"+f.Name), path+"\"+fmt.Sprintf"%s%s%s", filenameOnly, "1", fileExt)) //将每个文件名中的1替换为2,扩展名不变 os.Renamepath+"\"+f.Name), path+"\"+fmt.Sprintf"%s%s", strings.ReplacefilenameOnly, "1", "2", 1), fileExt)) } }
参考:https://studygolang.com/articles/14090
https://www.jianshu.com/p/c0aa30cb4bc0