@@ -66,6 +66,7 @@ func main() {
66
66
exclude := flag .String ("exclude" , "" , "Comma-separated list of extensions to exclude" )
67
67
verbose := flag .Bool ("verbose" , false , "Enable verbose output" )
68
68
dryrun := flag .Bool ("dryrun" , false , "Simulate the file organization without doing any actual changes" )
69
+ cleanAll := flag .Bool ("cleanAll" , false , "Delete all the empty folders and sub-folders in the specified directory after organizing files." )
69
70
70
71
flag .Parse ()
71
72
@@ -114,6 +115,7 @@ func main() {
114
115
fmt .Println (" -exclude <exts> : Comma-separated list of extensions to exclude." )
115
116
fmt .Println (" -verbose : Enable verbose output." )
116
117
fmt .Println (" -dryrun : Simulate the file organization without doing any actual changes." )
118
+ fmt .Println (" -cleanAll : Delete all the empty folders and sub-folders in the specified directory after organizing files." )
117
119
fmt .Println ("" )
118
120
return
119
121
}
@@ -189,36 +191,38 @@ func main() {
189
191
}
190
192
}
191
193
192
- fmt .Println ("\n Files organized by extension in" , * dir )
194
+ fmt .Printf ("\n Files organized by extension in %s \n " , * dir )
193
195
}
194
196
195
- // if *cleanAll {
196
- // cleanEmptyDirs("." )
197
- // }
197
+ if * cleanAll {
198
+ cleanEmptyDirs (* dir , * dryrun )
199
+ }
198
200
}
199
201
200
- // func cleanEmptyDirs(dir string) {
201
- // files, err := os.ReadDir(dir)
202
- // if err != nil {
203
- // fmt.Println("Error reading directory:", err)
204
- // return
205
- // }
206
-
207
- // for _, file := range files {
208
- // if file.IsDir() {
209
- // subDir := filepath.Join(dir, file.Name())
210
- // cleanEmptyDirs(subDir)
211
- // subFiles, err := os.ReadDir(subDir)
212
- // if err != nil {
213
- // fmt.Println("Error reading subdirectory:", err)
214
- // continue
215
- // }
216
- // if len(subFiles) == 0 {
217
- // fmt.Printf("Deleting empty directory: %s\n", subDir)
218
- // if err := os.Remove(subDir); err != nil {
219
- // fmt.Println("Error deleting directory:", err)
220
- // }
221
- // }
222
- // }
223
- // }
224
- // }
202
+ func cleanEmptyDirs (dir string , dryrun bool ) {
203
+ files , err := os .ReadDir (dir )
204
+ if err != nil {
205
+ fmt .Println ("Error reading directory:" , err )
206
+ return
207
+ }
208
+
209
+ for _ , file := range files {
210
+ if file .IsDir () {
211
+ subDir := filepath .Join (dir , file .Name ())
212
+ cleanEmptyDirs (subDir , dryrun )
213
+ subFiles , err := os .ReadDir (subDir )
214
+ if err != nil {
215
+ fmt .Println ("Error reading subdirectory:" , err )
216
+ continue
217
+ }
218
+ if len (subFiles ) == 0 {
219
+ fmt .Printf ("Deleting empty directory: %s\n " , subDir )
220
+ if ! dryrun {
221
+ if err := os .Remove (subDir ); err != nil {
222
+ fmt .Println ("Error deleting directory:" , err )
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+ }
0 commit comments