Groovy: Rename files recursively
Published on 29 July 2019
My video player on my TV has its way of passing
from one file to another in continuous reading
and its playback mode consists of going from 1…..mp4 to 10….mp4,
and not from 1…mp4 to 2….mp4.
The problem is that there are nearly 900 files and folders.
Bingo a great use case of Groovy for manipulating
files, renaming, pattern search in the file name
or file.
Here the goal is to change names starting with
a digit and whose second character of the name
is not a digit, I will add a 0 at the beginning of this name
renameFilesToStartWithTwoDigit.groovy
#!/usr/bin/env groovy
/**
* get a crossplateform separator
* @return
*/
static String getSeparator() {
System.getProperty("file.separator")
}
/**
* rename file
* @param file
* @param newName
* @return the renamed file
*/
static File rename(File file, String newName) {
assert file.renameTo(file
.parentFile.path +
getSeparator() +
newName)
new File(file
.parentFile.path +
getSeparator() +
newName)
}
String rootFolderPath = System.getProperty("user.home") +
"/Bureau/ng-courses/Udemy - Angular 7 " +
"(formerly Angular 2) - The Complete Guide"
File rootFolder = new File(rootFolderPath)
assert rootFolder.exists()
assert rootFolder.directory
assert rootFolder.canRead()
List<File> dirList = new ArrayList<>()
//Adding rootFolder in dirList
// just in case there is some files in it
dirList.add(rootFolder)
//looping to get list of directories
//recursively inside rootFolder
rootFolder.traverse { File file ->
!file.directory ?: dirList.add(file)
}
//looping through dirList
//to rename directories filling criterias
for (int i = dirList.size() - 1; i >= 0; i--) {
Character[] chars = dirList.get(i).name.chars
!(chars[0].digit && !chars[1].digit) ?:
dirList.set(i, rename(dirList.get(i),
"0${dirList.get(i).name}"))
if (dirList.get(i).directory) {
//looping through directory
//to rename file filling criterias
dirList.get(i).listFiles().each { File f ->
Character[] cs = f.name.chars
!(cs[0].digit && !cs[1].digit) ?:
rename(f, "0${f.name}")
}
}
}