Skip to content

Instantly share code, notes, and snippets.

@danieldbower
Created June 8, 2011 13:12
Show Gist options
  • Save danieldbower/1014390 to your computer and use it in GitHub Desktop.
Save danieldbower/1014390 to your computer and use it in GitHub Desktop.
Rename Droid X picture files Groovy
/*
* motorolaDate.groovy
* Renames photos taken by Droid X Cameras into something
* more sortable and useful.
*
* runs with groovy http://groovy.codehaus.org/
*
* Download, edit the dirName variable, and then run:
* groovy motorolaDate.groovy
* The script will create sub-directories by year and month.
*/
def dirName = '/home/user1/Pictures/new/droidx'
def dir = new File(dirName)
dir.eachFile{
if (it.isFile()){
//matches structure of motorola pics?
if(!it.name.matches("[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}_[0-9]{2}\\-[0-9]{2}\\-[0-9]{2}_[0-9]+.jpg") &&
!it.name.matches("[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}_[0-9]{2}\\-[0-9]{2}\\-[0-9]{2}_[0-9]+.3gp")){
return
}
def periods = it.name.tokenize('.')
def underscored = periods[0].tokenize('_')
def day = underscored[0].replace('-', '')
def time = underscored[1].tokenize('-')
File newFile = new File(dirName + "/" + day + '_' + time[0] + time[1] + '_' + time[2] + '_' + underscored[2] + '.' + periods[periods.size -1])
it.renameTo(newFile)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment