Advertisement
kolinsol

прупупу

Dec 28th, 2016
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package files;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6.  
  7. public class Files
  8. {
  9.     public static String root_dir = "/Users/kolinsol/Desktop/_temp_downloads";
  10.     public static void main(String[] args)
  11.     {
  12.         File root_folder = new File(root_dir);
  13.         dsCheck(root_dir);
  14.         filrNamesList(root_folder);
  15.         System.out.println(dirSize(root_folder));
  16.         ArrayList<File> files = new ArrayList<>(Arrays.asList(root_folder.listFiles()));
  17.         String[] file_names = fileNamesToString(root_folder.listFiles());
  18.         for (String file_name : file_names)
  19.         {
  20.             System.out.println(file_name);
  21.         }
  22.         fileRenamer(root_folder.listFiles(),file_names);  
  23.     }
  24.    
  25.     public static void dsCheck(String path)
  26.     {
  27.         try
  28.         {
  29.             File ds_store = new File (path+"/.DS_Store");
  30.             if (ds_store.exists())
  31.             {
  32.                 ds_store.delete();
  33.                 System.out.println("ds_store deleted.");
  34.             }
  35.             else
  36.             {
  37.                 System.out.println("no ds_store.");
  38.             }
  39.         }
  40.         catch (NullPointerException e)
  41.         {
  42.             System.out.println("wrong filename.");
  43.         }
  44.     }
  45.    
  46.     public static void filrNamesList(File dir)
  47.     {
  48.         try
  49.         {
  50.             for (File child:dir.listFiles())
  51.                 System.out.println(child.toString());
  52.         }
  53.         catch (NullPointerException e)
  54.         {
  55.             System.out.println("the folder is empty.");
  56.         }
  57.     }
  58.  
  59.     public static int dirSize (File dir)
  60.     {
  61.         return dir.listFiles().length;
  62.     }
  63.    
  64.     public static String[] fileNamesToString(File[] files)
  65.     {
  66.         String[] file_names = new String[files.length];
  67.         for (int i=0; i<file_names.length;i++)
  68.         {
  69.             file_names[i]=files[i].toString();
  70.             file_names[i] = file_names[i].replaceAll(root_dir+"/", "");
  71.             file_names[i] = file_names[i].replace("[www.MP3Fiber.com]","");
  72.             file_names[i] = file_names[i].replace("_", " ");
  73.             file_names[i] = file_names[i].toLowerCase();
  74.             file_names[i] = file_names[i].replace("prod", " p/ ");
  75.             file_names[i] = file_names[i].replace("feat", " w/ ");
  76.             file_names[i] = file_names[i].replace("production", " p/ ");
  77.             file_names[i] = file_names[i].replace("featuring", " w/ ");
  78.             file_names[i] = file_names[i].replace("ft", " w/ ");
  79.             file_names[i] = file_names[i].replace("  ", " ");
  80.         }
  81.         return(file_names);
  82.     }
  83.    
  84.     public static void fileRenamer(File[] files, String[] file_names)
  85.     {
  86.         if(files.length==file_names.length)
  87.         {
  88.             for(int i=0; i<files.length;i++)
  89.                 files[i].renameTo(new File(root_dir+"/"+file_names[i]));
  90.         }
  91.         else
  92.         {
  93.             System.out.println("wrong nuber of files.");
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement