ZBLOG

golang cmd 将ConfigFilePath整个文件夹中的配置重命名,加上后缀.bak

您可以使用以下 Go 代码来实现将指定文件夹中的所有配置文件重命名为带有”.bak”后缀的新文件名:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. func main() {
  10. configFolderPath := "ConfigFilePath" // 指定配置文件夹路径
  11. files, err := ioutil.ReadDir(configFolderPath)
  12. if err != nil {
  13. fmt.Println("Error reading directory:", err)
  14. return
  15. }
  16. for _, file := range files {
  17. if !file.IsDir() && strings.HasSuffix(file.Name(), ".conf") { // 只处理.conf结尾的配置文件
  18. oldFilePath := filepath.Join(configFolderPath, file.Name())
  19. newFileName := file.Name() + ".bak"
  20. newFilePath := filepath.Join(configFolderPath, newFileName)
  21. err := os.Rename(oldFilePath, newFilePath)
  22. if err != nil {
  23. fmt.Printf("Error renaming %s: %v\n", oldFilePath, err)
  24. } else {
  25. fmt.Printf("Renamed %s to %s\n", oldFilePath, newFilePath)
  26. }
  27. }
  28. }
  29. }

在这段代码中,您需要将"ConfigFilePath"替换为您想要操作的目标文件夹路径。该程序会遍历指定文件夹中所有以.conf结尾的配置文件,并将它们重命名为原文件名加上.bak后缀。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=18557

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?