Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Created February 12, 2020 02:26
Show Gist options
  • Save guizmaii/e3ae51956277dbabf688c87fb460e219 to your computer and use it in GitHub Desktop.
Save guizmaii/e3ae51956277dbabf688c87fb460e219 to your computer and use it in GitHub Desktop.
/**
* Ammonite script to rename images.
*
* This script will lower case the file names and replace all the spaces and dots by `_`.
*
* Source dir and destination dir have to be different because some file systems don't consider the cases.
* So a file name `ABC.png` will not be renamed `abc.png` if the source and destination are the same...s
*/
import $ivy.`com.github.pathikrit::better-files:3.8.0`
import better.files._
import java.nio.file.Files
val sourceDir: File = file"path/to/files/"
val destDir: File = file"renamed/files/destination/path/must/be/different/than/source"
sourceDir.entries.zipWithIndex.foreach { case (file, i) =>
val extension = file.extension.get
val newName = file
.name
.split("\\.png")
.head
.toLowerCase
.replaceAll("\\.", "_")
.replaceAll(" ", "_")
println(i + ": " + file.name + " - " + newName)
Files.move(file.path, (destDir / s"$newName$extension").path, File.CopyOptions(overwrite = true): _*)
}
println("Hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment