Last active
August 26, 2016 19:11
-
-
Save flowerinthenight/f8114f79b6699ced9a054a595b6c991a to your computer and use it in GitHub Desktop.
A simple directory cleanup tool for Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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