main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. var (
  10. KEEP_SRT bool = false
  11. )
  12. func scan_dir(dir string) ([]string, error) {
  13. var r []string = []string{}
  14. ls, err := os.ReadDir(dir)
  15. if err != nil {
  16. return nil, err
  17. }
  18. for _, elem := range ls {
  19. if elem.IsDir() {
  20. rs, err := scan_dir(filepath.Join(dir, elem.Name()))
  21. if err != nil {
  22. return nil, err
  23. }
  24. for _, e := range rs {
  25. r = append(r, filepath.Join(elem.Name(), e))
  26. }
  27. } else {
  28. if strings.HasSuffix(elem.Name(), ".srt") {
  29. if !KEEP_SRT {
  30. continue
  31. }
  32. r = append(r, elem.Name())
  33. continue
  34. }
  35. if strings.HasSuffix(elem.Name(), ".info") || strings.HasSuffix(elem.Name(), ".txt") {
  36. continue
  37. }
  38. r = append(r, elem.Name())
  39. }
  40. }
  41. return r, nil
  42. }
  43. func is_empty(dir string) bool {
  44. ls, err := os.ReadDir(dir)
  45. if err != nil {
  46. return false
  47. }
  48. for _, elem := range ls {
  49. if elem.IsDir() {
  50. return is_empty(filepath.Join(dir, elem.Name()))
  51. }
  52. if strings.HasSuffix(elem.Name(), ".srt") {
  53. if !KEEP_SRT {
  54. continue
  55. }
  56. return false
  57. }
  58. if strings.HasSuffix(elem.Name(), ".info") || strings.HasSuffix(elem.Name(), ".txt") {
  59. continue
  60. }
  61. return false
  62. }
  63. return true
  64. }
  65. func scan_for_dirs(dir string) ([]string, error) {
  66. var r []string = []string{}
  67. ls, err := os.ReadDir(dir)
  68. if err != nil {
  69. return nil, err
  70. }
  71. for _, elem := range ls {
  72. if elem.IsDir() && is_empty(filepath.Join(dir, elem.Name())) {
  73. r = append(r, filepath.Join(dir, elem.Name()))
  74. }
  75. }
  76. return r, nil
  77. }
  78. func main() {
  79. var REMOVE bool = true
  80. // Keeps
  81. if len(os.Args) != 1 {
  82. for idx, arg := range os.Args {
  83. if idx == 0 { // Skip program name
  84. continue
  85. }
  86. switch strings.ToLower(arg) {
  87. case "srt":
  88. KEEP_SRT = true
  89. case "norm", "no-rm", "no-remove":
  90. REMOVE = false
  91. case "help", "h", "?":
  92. fmt.Println("--- Help ---")
  93. fmt.Println("undir will move 1 or more files from a directory, then delete the directory.")
  94. fmt.Println("")
  95. fmt.Println("Commands:")
  96. fmt.Println("")
  97. fmt.Println(" srt undir will move *.srt files")
  98. fmt.Println(" norm, no-rm, no-remove undir will NOT remove directories or files")
  99. fmt.Println(" help, h, ? Prints this help screen and exits (code 0, success)")
  100. fmt.Println("")
  101. fmt.Println("Example Usage:")
  102. fmt.Println("")
  103. fmt.Println("undir Standard run of undir, will move any file NOT *.info or *.srt")
  104. fmt.Println("undir srt Run undir but move *.srt files")
  105. fmt.Println("undir no-rm Run undir but do NOT remove directories or files, simply copy them")
  106. fmt.Println("undir srt norm Run undir but move *.srt files, also do NOT remove directories or files, simply copy them")
  107. fmt.Println("undir ? Prints this help screen and exits (code 0, success)")
  108. os.Exit(0)
  109. }
  110. }
  111. if KEEP_SRT {
  112. fmt.Println("Keeping .srt's")
  113. }
  114. if !REMOVE {
  115. fmt.Println("No Remove Mode")
  116. }
  117. }
  118. actions, err := scan_dir(".")
  119. if err != nil {
  120. fmt.Println("Err:", err)
  121. return
  122. }
  123. fmt.Println("Found:", len(actions))
  124. for idx, a := range actions {
  125. fmt.Println(a)
  126. out, err := os.Create(filepath.Base(a))
  127. if err != nil {
  128. fmt.Println("Err (Create out):", err)
  129. continue
  130. }
  131. in, err := os.Open(a)
  132. if err != nil {
  133. out.Close()
  134. fmt.Println("RM", filepath.Base(a))
  135. err1 := os.Remove(filepath.Base(a))
  136. if err1 != nil {
  137. fmt.Println("Err (Cleanup):", err1)
  138. }
  139. fmt.Println("Err (Open in):", err)
  140. continue
  141. }
  142. _, err = io.Copy(out, in)
  143. if err != nil {
  144. in.Close()
  145. out.Close()
  146. fmt.Println("RM", filepath.Base(a))
  147. err1 := os.Remove(filepath.Base(a))
  148. if err1 != nil {
  149. fmt.Println("Err (Cleanup):", err1)
  150. }
  151. fmt.Println("Err (Copy in -> out):", err)
  152. continue
  153. }
  154. if idx+1 < len(actions) {
  155. fmt.Println("End?")
  156. if REMOVE {
  157. fmt.Println("RM", filepath.Dir(a))
  158. err = os.RemoveAll(filepath.Dir(a))
  159. if err != nil {
  160. fmt.Println("Err (Remove):", err)
  161. }
  162. var can_clean bool = true
  163. for _, next := range actions[idx+1:] {
  164. if filepath.Dir(next) == filepath.Dir(a) {
  165. can_clean = false
  166. }
  167. }
  168. if can_clean {
  169. new_scan, err := scan_for_dirs(".")
  170. if err != nil {
  171. fmt.Println("Err (Rescan):", err)
  172. continue
  173. }
  174. if len(new_scan) != 0 {
  175. for _, elem := range new_scan {
  176. fmt.Println("RM", elem)
  177. err = os.RemoveAll(elem)
  178. if err != nil {
  179. fmt.Println("Err (Remove):", err)
  180. }
  181. }
  182. }
  183. }
  184. }
  185. continue
  186. }
  187. var can_clean bool = true
  188. for _, next := range actions[idx+1:] {
  189. if filepath.Dir(next) == filepath.Dir(a) {
  190. can_clean = false
  191. }
  192. }
  193. if can_clean && REMOVE {
  194. fmt.Println("RM", filepath.Dir(a))
  195. err = os.RemoveAll(filepath.Dir(a))
  196. if err != nil {
  197. fmt.Println("Err (Remove):", err)
  198. continue
  199. }
  200. new_scan, err := scan_for_dirs(".")
  201. if err != nil {
  202. fmt.Println("Err (Rescan):", err)
  203. continue
  204. }
  205. if len(new_scan) != 0 {
  206. for _, elem := range new_scan {
  207. fmt.Println("RM", elem)
  208. err = os.RemoveAll(elem)
  209. if err != nil {
  210. fmt.Println("Err (Remove):", err)
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }