您可以使用以下代码片段来实现将指定文件夹中的配置文件重命名,并加上前缀”backup_“:
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
dirPath := "path/to/your/directory" // 指定文件夹路径
files, err := ioutil.ReadDir(dirPath)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
for _, file := range files {
if !file.IsDir() {
oldFilePath := filepath.Join(dirPath, file.Name())
newFilePath := filepath.Join(dirPath, "backup_"+file.Name())
err := os.Rename(oldFilePath, newFilePath)
if err != nil {
fmt.Println("Error renaming file:", err)
} else {
fmt.Printf("Renamed %s to %s\n", oldFilePath, newFilePath)
}
}
}
}
请将”path/to/your/directory”替换为您要操作的目标文件夹的路径。这段代码会遍历指定文件夹中的所有文件,对每个文件进行重命名操作。
内容由零声教学AI助手提供,问题来源于学员提问