[Golang] Find Redundant Files Saved by Chrome


Find redundant files saved by Chrome browser via Go, and then delete them.

chrome.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package fr

import (
	"os"
	"path/filepath"
	"regexp"
)

var redundant = regexp.MustCompile(` \(\d+\)$`)

func isRedundant(filename string) bool {
	ext := filepath.Ext(filename)
	filenameWithoutExt := filename[:len(filename)-len(ext)]
	if redundant.Match([]byte(filenameWithoutExt)) {
		return true
	}
	return false
}

func removeFile(path string) {
	println("Remove " + path)
	os.Remove(path)
}

func FindRedundant(dirPath string) {
	// walk all files in directory
	filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			if isRedundant(info.Name()) {
				removeFile(path)
			}
		}
		return nil
	})
}
chrome_test.go | repository | view raw
1
2
3
4
5
6
7
8
9
package fr

import (
	"testing"
)

func TestFindRedundant(t *testing.T) {
	FindRedundant("/home/foo/Downloads/")
}

Tested on: Ubuntu Linux 16.04, Go 1.6.2.


References:

[1][Python] Find Redundant Files Saved by Chrome
[2][Golang] Walk All Files in Directory
[3]
[4]
[5]