Selaa lähdekoodia

Initial commit

zhjs 1 vuosi sitten
commit
dbacaac3ae
8 muutettua tiedostoa jossa 119 lisäystä ja 0 poistoa
  1. 8 0
      .idea/.gitignore
  2. 9 0
      .idea/delog.iml
  3. 8 0
      .idea/modules.xml
  4. 6 0
      .idea/vcs.xml
  5. BIN
      delog.exe
  6. 81 0
      delog.go
  7. 5 0
      go.mod
  8. 2 0
      go.sum

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 9 - 0
.idea/delog.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/delog.iml" filepath="$PROJECT_DIR$/.idea/delog.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

BIN
delog.exe


+ 81 - 0
delog.go

@@ -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()
+}

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module delog
+
+go 1.20
+
+require github.com/robfig/cron/v3 v3.0.1

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
+github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=