Last active
August 29, 2015 14:13
-
-
Save gong023/3113f39cbe793fa4253c to your computer and use it in GitHub Desktop.
手元の同人整理する
This file contains 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 ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"regexp" | |
"runtime" | |
"sync" | |
) | |
func main() { | |
files, err := filepath.Glob("./*.pdf") | |
if err != nil { | |
fmt.Println(err) | |
} | |
re := regexp.MustCompile(`^\[(.+?)\]`) | |
circleMap := map[string][]string{} | |
for _, file := range files { | |
if !re.MatchString(file) { | |
continue | |
} | |
circle := re.FindAllStringSubmatch(file, 1)[0][1] | |
circles, ok := circleMap[circle] | |
if ok { | |
circleMap[circle] = append(circles, file) | |
} else { | |
circleMap[circle] = []string{file} | |
} | |
} | |
var wg sync.WaitGroup | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
for circle, files := range circleMap { | |
wg.Add(1) | |
if len(files) == 1 { | |
wg.Done() | |
continue | |
} | |
os.Mkdir("./"+circle, 0755) | |
for _, file := range files { | |
if err := os.Rename("./"+file, "./"+circle+"/"+file); err != nil { | |
fmt.Println(err) | |
} | |
} | |
wg.Done() | |
} | |
wg.Wait() | |
fmt.Println("done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment