kopi house
Monday, December 3, 2007
  Quick file rename with Groovy
Recently I have been learning the syntax of regular expression. Now regular expression + (Groovy of course) become part of my 'toolset' for my daily computer usage. A very quick and easy demonstration of what I meant here..

Lets say you downloaded you favourite TV shows and you wish to rename all your .avi files into a standardize format.

E.g. You have the following files
D:\Heroes\Heroes.S01E12.HDTV-LOL.avi
D:\Heroes\heroes.S01E13.HDTV.XviD-FPN.avi
D:\Heroes\Heroes.S01E14.HDTV-LOL.avi

instead of renaming the file one-by-one, use the following code



def sourceFolder = 'D:\\Heroes'
new File(sourceFolder).eachFile {
def p = /[hH](eroes)\.S?0?(\d)E?(\d\d?).*/
if ( it.getName() ==~ p ) {
def s = (it.getName() =~ p).replaceFirst('H$1.S0$2E$3.avi')
println "${it.getName()}\n${it.getParent().toString()}\\${s}\n"
assert it.renameTo(new File("${it.getParent().toString()}\\${s}"))
}
}


Your files should be rename to
D:\Heroes\Heroes.S01E12.avi
D:\Heroes\Heroes.S01E13.avi
D:\Heroes\Heroes.S01E14.avi

Labels:

 

Name:

Why kopi house? The word kopi means coffee in our dialect and since Java symbolic icon is a cup of coffee, so this is why I named my blog as kopi house (Java with local favour) ; )

Archives
December 2007 / May 2008 / February 2009 / March 2009 / August 2009 /


Powered by Blogger

Subscribe to
Posts [Atom]