markoczy

movetosubdir

Apr 26th, 2021 (edited)
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.12 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "flag"
  5.     "io/ioutil"
  6.     "log"
  7.     "os"
  8.     "path"
  9.     "path/filepath"
  10.     "sort"
  11.     "strconv"
  12.     "strings"
  13.     "time"
  14. )
  15.  
  16. var (
  17.     // flags
  18.     root  string
  19.     limit int
  20.     pad   int
  21.     order string
  22.     // operation vars
  23.     folder = 1
  24. )
  25.  
  26. type fileInfo struct {
  27.     path    string
  28.     modTime time.Time
  29. }
  30.  
  31. func initFlags() {
  32.     rootPtr := flag.String("root", ".", "Root directory")
  33.     limitPtr := flag.Int("limit", 100, "Max files per folder")
  34.     padPtr := flag.Int("pad", 4, "Pad zeroes until length")
  35.     orderPtr := flag.String("order", "date", "How to order files, possible values: date, name")
  36.     flag.Parse()
  37.     root = *rootPtr
  38.     limit = *limitPtr
  39.     order = *orderPtr
  40.     pad = *padPtr
  41. }
  42.  
  43. func getFiles(limit int) []fileInfo {
  44.     ret := []fileInfo{}
  45.     // filepath.
  46.     fi, err := ioutil.ReadDir(root)
  47.     check(err)
  48.     for _, info := range fi {
  49.         if info.IsDir() {
  50.             continue
  51.         }
  52.         curInfo := fileInfo{
  53.             path:    path.Join(root, info.Name()),
  54.             modTime: info.ModTime(),
  55.         }
  56.         ret = append(ret, curInfo)
  57.     }
  58.     if len(ret) == 0 {
  59.         return ret
  60.     }
  61.     sort.SliceStable(ret, func(i, j int) bool {
  62.         if order == "name" {
  63.             return strings.Compare(ret[i].path, ret[j].path) < 0
  64.         }
  65.         return ret[i].modTime.Unix() < ret[j].modTime.Unix()
  66.     })
  67.     return ret[:min(len(ret), limit)]
  68. }
  69.  
  70. func check(err error) {
  71.     if err != nil {
  72.         panic(err)
  73.     }
  74. }
  75.  
  76. func min(a ...int) int {
  77.     if len(a) == 0 {
  78.         return 0
  79.     }
  80.     ret := a[0]
  81.     for _, v := range a[1:] {
  82.         if v < ret {
  83.             ret = v
  84.         }
  85.     }
  86.     return ret
  87. }
  88.  
  89. func padZero(num, length int) string {
  90.     numStr := strconv.Itoa(num)
  91.     for length > len(numStr) {
  92.         numStr = "0" + numStr
  93.     }
  94.     return numStr
  95. }
  96.  
  97. func main() {
  98.     initFlags()
  99.     files := getFiles(limit)
  100.     for len(files) > 0 {
  101.         for _, fi := range files {
  102.             dir, _ := filepath.Abs(filepath.Dir(fi.path))
  103.             targetDir := filepath.Join(dir, padZero(folder, pad))
  104.             log.Println("Creating target:", targetDir)
  105.             _ = os.Mkdir(targetDir, 0644)
  106.             target := filepath.Join(targetDir, filepath.Base(fi.path))
  107.             log.Println("Moving file", fi.path, "to", target)
  108.             check(os.Rename(fi.path, target))
  109.         }
  110.         folder++
  111.         files = getFiles(limit)
  112.     }
  113. }
  114.  
Add Comment
Please, Sign In to add comment