Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created Mar 22, 2021
Embed
What would you like to do?
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