Created
January 20, 2020 17:13
-
-
Save eslam-mahmoud/d6bf07ea681f0bce1c816d6408df0248 to your computer and use it in GitHub Desktop.
split images into folders - backup
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 ( | |
"io/ioutil" | |
"log" | |
"os" | |
) | |
func main() { | |
files, err := ioutil.ReadDir("./Camera") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, f := range files { | |
switch mode := f.Mode(); { | |
// if not file ignore it | |
case !mode.IsDir(): | |
continue | |
} | |
fileName := f.Name() | |
// fmt.Println(fileName) | |
folderName := fileName[0:4] + "-" + fileName[4:6] + "-" + fileName[6:8] | |
// fmt.Println(folderName) | |
createDirIfNotExist("./Camera/" + folderName) | |
// fmt.Println("./Camera/" + folderName + "/" + f.Name()) | |
moveFile("./Camera/"+f.Name(), "./Camera/"+folderName+"/"+f.Name()) | |
} | |
} | |
func createDirIfNotExist(dir string) { | |
if _, err := os.Stat(dir); os.IsNotExist(err) { | |
err = os.MkdirAll(dir, 0755) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
func moveFile(src string, dist string) { | |
err := os.Rename(src, dist) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment