Skip to content

The trash-go is the library for golang to move specified files to trashbox

License

Notifications You must be signed in to change notification settings

hymkor/trash-go

Repository files navigation

trash-go

trash-go is a Go library for moving specified files to the trash (Recycle Bin/Trash Can) on Microsoft Windows.

package trash // import "github.com/hymkor/trash-go"

func Throw(filenames ...string) error

Non-Windows environments (experimental)

trash.Throw moves files to "the home trash" of the FreeDesktop.org Trash specification 1.0.

Example

./example.go

package main

import (
    "fmt"
    "os"
    "path/filepath"

    "github.com/hymkor/trash-go"
)

func mains(args []string) error {
    filenames := make([]string, 0, len(args))
    for _, arg := range args {
        if matches, err := filepath.Glob(arg); err != nil {
            filenames = append(filenames, arg)
        } else if len(matches) < 1 {
            return fmt.Errorf("%s: %w", arg, os.ErrNotExist)
        } else {
            filenames = append(filenames, matches...)
        }
    }
    return trash.Throw(filenames...)
}

func main() {
    if err := mains(os.Args[1:]); err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(1)
    }
}

See also