import groovy.io.FileType FileCollection collection File srcDir = new File('Taylor Swift') task rename { println 'pwd'.execute().text def list = [] srcDir.eachFileRecurse(FileType.FILES) { file -> list << file } list.each { changeName(it) } } def changeName(File file) { String regex = "^[0-9][0-9].*" String originalName = file.getName() boolean b = originalName.matches(regex); if (b) { String newname = originalName.replaceFirst("[0-9][0-9]\\s", "") File newNameFile = new File(file.getParent(), newname) file.renameTo(newNameFile) println newNameFile } }
Saturday, December 5, 2015
Use Gradle to remove 2-digits prefix in filenames recursively
Script to change all name prefix in a directory
The unix script to remove all file name prefix of 2 digits. example :
03 What is this about.txt
for name in [0-9][0-9]*
do
newname="$(echo "$name" | cut -c4-)"
mv "$name" "$newname"
echo ${newname}
done
This uses bash command substitution to remove the first 3 characters from the input filename via
cut
, and stores that in $newname
. Then it renames the old name to the new name. This is performed on every file.cut -c4-
specifies that only characters after index 4 should be returned from the input. 4-
is a range starting at index 7 with no end; that is, until the end of the line.Reference:
http://unix.stackexchange.com/questions/47367/bulk-rename-change-prefix
Subscribe to:
Posts (Atom)