Today, when I wanted to create a batch tool for file names, I wrote a temporary tool that supports batch suffix modification and file name numerical formatting.
Import Java. io. file; public class alternameutil {// modify the file name tool public alternameutil () {}/ * modify the extension name */public static int Index = 1; public static void alternameext (string filepath, string sext, string Dext) {int last; file temp; file F = new file (filepath); If (! F. exists () {system. Out. println ("The path file or folder you entered does not exist! "); Return;} else {If (F. isfile () {If (F. getname (). lastindexof (sext)> 0) {system. out. print (F. getname () + "found! "); String diretory = filepath. substring (0, filepath. lastindexof ("/") + 1); last = f. getname (). lastindexof (". "); temp = new file (diretory + F. getname (). substring (0, last) + Dext); If (F. renameto (temp) {system. out. println (temp. getname () + "modified successfully");} else {system. out. println ("modification failed! ") ;}} Else if (F. isdirectory () {// direpaths = filepath. substring (0, filepath. lastindexof ("/"); string [] filenames = f. list (); For (INT I = 0; I <filenames. length; I ++) {alternameext (filepath + "/" + filenames [I], sext, Dext );}}}} // format file name numeric format public static void altername (string filepath, string format, string sext) {int last; file temp; file F = new file (filepath); If (! F. exists () {system. Out. println ("The path file or folder you entered does not exist! "); Return;} else {If (F. isfile () {If (F. getname (). indexof (sext)> 0) {system. out. print (F. getname () + "found! "); String diretory = filepath. substring (0, filepath. lastindexof ("/") + 1); last = f. getname (). lastindexof (". "); temp = new file (diretory + format +" _ "+ (++ index) + sext); If (F. renameto (temp) {system. out. println (temp. getname () + "modified successfully");} else {system. out. println ("modification failed! ") ;}} Else if (F. isdirectory () {// direpaths = filepath. substring (0, filepath. lastindexof ("/"); string [] filenames = f. list (); For (INT I = 0; I <filenames. length; I ++) {altername (filepath + "/" + filenames [I], format, sext) ;}}} public static void main (string [] ARGs) {alternameext ("D: // Res/layout ",". java ",". XML "); altername (" D: // Res/layout "," Main ",". XML ");}}