Traversing through files with Groovy and renaming specific file types
I have been working with Groovy and Grails for around a year now, learnt it for official project purpose. But Never have kept common problem solutions documented. I have faced such kind of problem earlier, but this time I am writing. It is known that Groovy runs on top of JVM. Java is a very popular and powerful language. Groovy has inherited all good features from Java but also has added more beautiful features to it’s runtime. Hence, I became surprised when I first used this language. If you haven’t worked with yet, you should give it a try. Enough talk, the code snippet is given below.
String pathName = "F:\\Udemy Tutorials\\[FTUForum.com] Udemy - Go Full Stack with Spring Boot and Angular 7\\";
new File(pathName).eachFile() { file ->
if (file.listFiles()) {
file.eachFile { file2 ->
if (file2.name.contains(".fdmdownload")) {
println file2.name.split(".fdmdownload")[0]
file2.renameTo file2.name.split(".fdmdownload")[0]
}
}
}
}
Please feel free to suggest me if you have any better solution. Thanks in advance :)