Trình phát video trên TV của tôi có cách chuyển tiếp
từ tệp này sang tệp khác ở chế độ đọc liên tục
và chế độ phát của nó là chuyển từ 1…​..mp4 sang 10…​.mp4,
và không phải từ 1…​mp4 đến 2…​.mp4.
Vấn đề là có gần 900 tệp và thư mục.
Bingo, một trường hợp sử dụng tuyệt vời của Groovy để thao tác
các tệp, đổi tên, tìm kiếm mẫu trong tên tệp
hoặc thư mục.
Ở đây mục tiêu là thay đổi các tên bắt đầu bằng
một chữ số và ký tự thứ hai của tên
không phải là một chữ số, tôi sẽ thêm một số 0 vào đầu tên này

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}")
        }
    }
}

Bài viết liên quan