Created
December 30, 2013 14:27
-
-
Save siongui/8182656 to your computer and use it in GitHub Desktop.
刪除重覆的照片/文件 From:
http://www.oschina.net/code/snippet_116701_27642
This file contains hidden or 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 ( | |
"container/list" | |
"crypto/md5" | |
// "errors" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"path/filepath" | |
) | |
type FileInfo struct { | |
path string | |
// name string | |
md5 string | |
} | |
var file_list map[string]FileInfo | |
var file_delete_list list.List | |
func getFileInfo(path string) bool { | |
md5, err := calMd5(path) | |
if err != nil { | |
return false | |
} | |
var info FileInfo | |
info.md5 = md5 | |
info.path = path | |
addInfo(info) | |
return true | |
} | |
func addInfo(info FileInfo) { | |
_, ok := file_list[info.md5] | |
if ok { | |
file_delete_list.PushBack(info.path) | |
} else { | |
file_list[info.md5] = info | |
} | |
} | |
func doInfo(path string, info os.FileInfo, err error) error { | |
if info == nil { | |
fmt.Println(path + " info is nil") | |
return err | |
} | |
if info.IsDir() { | |
// if return not nil ,walk func will break | |
return nil | |
} | |
getFileInfo(path) | |
return nil | |
} | |
func do_dir(path string) error { | |
err := filepath.Walk(path, doInfo) | |
return err | |
} | |
func calMd5(path string) (md5_str string, er error) { | |
file, err := os.Open(path) | |
if err != nil { | |
fmt.Println(err) | |
return md5_str, err | |
} | |
h := md5.New() | |
io.Copy(h, file) | |
md5_bytes := h.Sum(nil) | |
md5_str = fmt.Sprintf("%X", md5_bytes) | |
return md5_str, err | |
} | |
func main() { | |
flag.Parse() | |
argc := flag.NArg() | |
// file_list = map[string]FileInfo | |
file_list = make(map[string]FileInfo) | |
fmt.Println(file_list) | |
fmt.Println(argc) | |
folder_list := list.New() | |
for i := 0; i != argc; i++ { | |
folder_list.PushBack(flag.Arg(i)) | |
} | |
for folder := folder_list.Front(); folder != nil; folder = folder.Next() { | |
root := folder.Value.(string) | |
fmt.Println(root) | |
err := do_dir(root) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
fmt.Println("File to delete is :") | |
for path := file_delete_list.Front(); path != nil; path = path.Next() { | |
fmt.Println("deleteing " + path.Value.(string)) | |
os.Remove(path.Value.(string)) | |
} | |
// for _, info := range file_list { | |
// fmt.Println(info.path) | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment