|
@@ -0,0 +1,81 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ cron "github.com/robfig/cron/v3"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 删除子目录下的以.html结尾的文件,创建时间早于keepDate的保留
|
|
|
+func deleteHTMLFiles(dirPath string, keepDate time.Time) {
|
|
|
+ files, err := ioutil.ReadDir(dirPath)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Error reading directory %s: %v\n", dirPath, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, file := range files {
|
|
|
+ if !file.IsDir() && filepath.Ext(file.Name()) == ".html" {
|
|
|
+ filePath := filepath.Join(dirPath, file.Name())
|
|
|
+ if file.ModTime().Before(keepDate) {
|
|
|
+ err := os.Remove(filePath)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Error deleting file %s: %v\n", filePath, err)
|
|
|
+ } else {
|
|
|
+ fmt.Printf("Deleted file: %s\n", filePath)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fmt.Printf("File %s is within the keep period. Skipping deletion.\n", filePath)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func start() {
|
|
|
+ // 获取当前目录的绝对路径
|
|
|
+ currentDir, err := os.Getwd()
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error getting current directory:", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当前日期和保留日期(前三天)
|
|
|
+ currentTime := time.Now()
|
|
|
+ keepDate := currentTime.AddDate(0, 0, -3)
|
|
|
+
|
|
|
+ // 遍历当前目录的子目录
|
|
|
+ subDirs, err := ioutil.ReadDir(currentDir)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error reading current directory:", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除子目录下的以.html结尾的文件
|
|
|
+ for _, subDir := range subDirs {
|
|
|
+ if subDir.IsDir() {
|
|
|
+ subDirPath := filepath.Join(currentDir, subDir.Name())
|
|
|
+ deleteHTMLFiles(subDirPath, keepDate)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("Deletion of .html files completed.")
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ // 获取当前目录的绝对路径
|
|
|
+ currentDir, _ := os.Getwd()
|
|
|
+ fmt.Print("currentDir=",currentDir,"\n")
|
|
|
+ c := cron.New()
|
|
|
+ i := 1
|
|
|
+ _, _ = c.AddFunc("0 0 */1 * *", func() {
|
|
|
+ start()
|
|
|
+ line := "\n--------------------------------------------------------\n"
|
|
|
+ fmt.Println(line, time.Now(), line)
|
|
|
+ i++
|
|
|
+ })
|
|
|
+ c.Run()
|
|
|
+}
|