Groovy: Rename files recursively
Published on 29 July 2019
The video player on my TV has its own way of moving from one file to another during continuous playback, and its playback mode is to go from 1…mp4 to 10…mp4, rather than from 1…mp4 to 2…mp4. The problem is that there are nearly 900 files and folders. Bingo: a great use case for Groovy to manipulate files, renaming them and searching for patterns in the file or folder name. Here, the goal is to change names that start with a digit where the second character is not a digit; I will add a 0 to the beginning of such names.
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}")
}
}
}