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