Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created March 22, 2021 01:29
Show Gist options
  • Save ychennay/c8ee6855fb42fdba35c947d70f688e40 to your computer and use it in GitHub Desktop.
Save ychennay/c8ee6855fb42fdba35c947d70f688e40 to your computer and use it in GitHub Desktop.
Simple Golang Desktop Cleaner
package main
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
)
const PATTERN = "Screen Shot *.png"
// cleanDesktop uses the Unix filepath to find the Desktop directory, then matches the screenshot filename pattern
// (Screen Shot *.png). It deletes each file that matches this pattern.
func cleanDesktop() {
fmt.Println("-------")
myself, userErr := user.Current()
if userErr != nil {
panic(userErr)
}
homedir := myself.HomeDir
desktop := homedir + "/Desktop/"
err := filepath.Walk(desktop, func(path string, info os.FileInfo, err error) error {
if matched, err := filepath.Match(PATTERN, filepath.Base(path)); err != nil {
return err
} else if matched {
fmt.Sprintf("Deleting %s.", path)
e := os.Remove(path)
if e != nil {
log.Fatal(e)
}
}
return nil
})
if err != nil {
panic(err)
}
}
func main() {
cleanDesktop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment