java code to rename all files in a folder

Solutions on MaxInterview for java code to rename all files in a folder by the best coders in the world

showing results for - "java code to rename all files in a folder"
Mohamed
05 Mar 2017
1// java code to rename all files in a folder
2import java.io.File;
3public class RenameAllFilesDemo 
4{
5   public static void main(String[] args) 
6   {
7      // folder path 
8      String strPath = "D:\\Users\\sachin\\java\\sachinfolder";
9      // let's create new folder
10      File newfolder = new File(strPath);
11      File[] arrFile = newfolder.listFiles();
12      for(int a = 0; a < arrFile.length; a++) 
13      {
14         if(arrFile[a].isFile()) 
15         {
16            File file = new File(strPath + "\\" + arrFile[a].getName()); 
17            String strFileName = arrFile[a].getName(); 
18            String[] tokens = strFileName.split("\s"); 
19            String strNewFile = tokens[1]; 
20            System.out.println(strFileName); 
21            System.out.print(strNewFile);
22            file.renameTo(new File(strPath + "\" + strNewFile + ".pdf")); 
23         } 
24      }
25   }
26}