Skip to content

Instantly share code, notes, and snippets.

@flowerinthenight
Last active August 26, 2016 19:11
Show Gist options
  • Save flowerinthenight/f8114f79b6699ced9a054a595b6c991a to your computer and use it in GitHub Desktop.
Save flowerinthenight/f8114f79b6699ced9a054a595b6c991a to your computer and use it in GitHub Desktop.
A simple directory cleanup tool for Windows.
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
)
func main() {
pathPtr := flag.String("path", "", "The `DIRPATH` to clean up. Only sub-items are inspected.")
beforePtr := flag.Int("sub", 30, "No. of `DAYS` ago. All items modified before that are deleted.")
flag.Parse()
if *pathPtr == "" {
panic("No -path provided.")
}
files, err := ioutil.ReadDir(*pathPtr)
if err != nil {
panic(err)
}
now := time.Now()
before := now.AddDate(0, 0, *beforePtr*-1)
log.Println("All items modified before", before.Format(time.UnixDate), "will be deleted.")
for _, f := range files {
p := *pathPtr + "\\" + f.Name()
mt, err := os.Stat(p)
if err != nil {
log.Println(err)
} else {
if mt.ModTime().Before(before) {
log.Println("Deleting", p, "...")
if mt.IsDir() {
c := exec.Command("cmd", "/C", "rmdir", "/s", "/q", p)
if err := c.Run(); err != nil {
log.Println(err)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment