Skip to content

Instantly share code, notes, and snippets.

@gong023
Last active August 29, 2015 14:13
Show Gist options
  • Save gong023/3113f39cbe793fa4253c to your computer and use it in GitHub Desktop.
Save gong023/3113f39cbe793fa4253c to your computer and use it in GitHub Desktop.
手元の同人整理する
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