animeic 2 years ago
parent
commit
fe86be3a6e

BIN
script/format-backup-file/format-backup-file


+ 3 - 0
script/format-backup-file/go.mod

@@ -0,0 +1,3 @@
+module format-backup-file
+
+go 1.19

+ 68 - 0
script/format-backup-file/main.go

@@ -0,0 +1,68 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path"
+	"regexp"
+	"strconv"
+	"strings"
+	"time"
+)
+
+func main() {
+	files, err := GetAllFile(".")
+	if err != nil {
+		fmt.Println(err)
+	}
+	fmt.Println(files)
+
+}
+func GetAllFile(pathname string) ([]string, error) {
+	result := []string{}
+
+	fis, err := ioutil.ReadDir(pathname)
+	if err != nil {
+		fmt.Printf("读取文件目录失败,pathname=%v, err=%v \n", pathname, err)
+		return result, err
+	}
+
+	// 所有文件/文件夹
+	for _, fi := range fis {
+		fullname := pathname + "/" + fi.Name()
+		// 是文件夹则递归进入获取;是文件,则压入数组
+		if fi.IsDir() {
+			temp, err := GetAllFile(fullname)
+			if err != nil {
+				fmt.Printf("读取文件目录失败,fullname=%v, err=%v", fullname, err)
+				return result, err
+			}
+			result = append(result, temp...)
+		} else {
+			// 匹配时间戳命名的文件
+			re := regexp.MustCompile(`\d{10}`)
+			strs := re.FindAllString(fullname, -1)
+			if len(strs) > 0 {
+				// 格式化匹配到的时间
+				backupTime := strs[0]
+				backup, _ := strconv.Atoi(backupTime)
+				tm := time.Unix(int64(backup), 0)
+				timestr := tm.Format("2006-01-02 15:04:05")
+
+				// 拼接需要重命名的文件带路径字符串
+				suffix := path.Ext(fullname)
+				fileSlice := strings.Split(fullname, "-")
+				filePrefix := fileSlice[:len(fileSlice)-1]
+				renamefile := strings.Join(filePrefix, "-") + "-" + timestr + suffix
+
+				// 重命名文件
+				os.Rename(fullname, renamefile)
+			}
+
+			result = append(result, fullname)
+		}
+	}
+
+	return result, nil
+}

+ 0 - 0
script/format-backup-file/qeentree-mongo[all]/455411.gz


+ 0 - 0
script/format-backup-file/qeentree-mongo[all]/qeentree-mongo[all]2022-11-28 00:45:00.gz


+ 0 - 0
script/format-backup-file/qeentree-mongo[all]/qeentree-mongo[all]2022-11-28 00:45:00.log