Directory / Folder Indexing Program Using Java

I thought it will be cool if I can make a program that scans some pen drive or hard disk connected to my PC in order find out how many images,videos or documents are there. In simple terms, a directory or folder/file indexing program. Thanks to java again.
Java Directory / Folder Indexer
Directory Indexer Main Window
Directory indexer uses the FILE class from java.io package. An object of FILE can store both files and directories. The parent or Root directory, from where we start indexing is read from the user using normal JTextField. Then a function called processDirectory() is called with argument as the given input text.
The function, processDirectory() then create a new File with argument as given path. Then the list of files and directories present inside the directory are taken as a file array using listFiles() method from File class. If you are interested in finding more about file listing, read How tocreate a file explorer in java.
 File[] listFiles = fl.listFiles();  
After getting the list of contents in the given directory, a for loop is applied which will go through all the available contents (objects of File class). As we said earlier, it can be either a file or a directory. If it is a file, then we check its extension by splitting the name string using “.” Symbol. The fun thing about extension is, a file can contain more than 1 dotes. For example, I want to name my file as “genuine.coder.image.jpg”. It is perfectly alright to use this filename and if we go for splitting and taking the string after first dot, we will get “.coder” which is definitely not what we are expecting. So the solution is to find the string after last dot and it is achieved using the code
String[] extension = listFile.getName().split("[.]");  
 String ex = extension[extension.length - 1]; 
So now we have the extension. All that we have to do is to check the string against the required extension (jpg,png,pdf etc) and increment the counter.
 if (ex.equalsIgnoreCase("jpg")) {  
    jpg.setText(Integer.parseInt(jpg.getText()) + 1 + "");  
 }  
I found this way of incrementing the counter as a simple one (not efficient at all) since I don’t have to maintain some arrays for each separate extensions.
If the file object which is being processed is not a file, then it may be a directory. If it is, then we have to get inside of that and index them also. A simple recursive call to the function will do the trick.
 if (listFile.isDirectory()) {  
      processDirectory(listFile.getAbsolutePath()); //Recursive call to processDirectoy  
 }  
I have used a thread to call the processDirectory() function. If I don’t, the updating on the UI (incrementing the counter) will be lagging. Using just one new thread, it is possible to achieve better refreshing and processing rates. For this purpose, a new class named directoryProcessor is created and implemented as Runnable which is then passed as a runnable object for new Thread object. The processDirectory is called from inside the run() method of directoryProcessor.
Class directoryProcessor
class directoryProcessor implements Runnable {  
     @Override  
     public void run() {  
       processDirectory(input.getText());  
       sts.setText("Completed");  
       JOptionPane.showMessageDialog(rootPane, "Indexing Completed", "Done", JOptionPane.INFORMATION_MESSAGE);  
     }  
   }  
Function processDirectory()
 
public void processDirectory(String dir) {  
     File fl = new File(dir);  
     sts.setText("Processing " + dir);  
     File[] listFiles = fl.listFiles();  
     for (File listFile : listFiles) {  
       if (listFile.isFile()) {  
         String[] extension = listFile.getName().split("[.]");  
         String ex = (extension[extension.length - 1]);  
         if (ex.equalsIgnoreCase("jpg")) {  
           jpg.setText(Integer.parseInt(jpg.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("png")) {  
           png.setText(Integer.parseInt(png.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("gif")) {  
           gif.setText(Integer.parseInt(gif.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("ppt")) {  
           ppt.setText(Integer.parseInt(ppt.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("doc")) {  
           doc.setText(Integer.parseInt(doc.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("pdf")) {  
           pdf.setText(Integer.parseInt(pdf.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("mp4")) {  
           mp4.setText(Integer.parseInt(mp4.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("flv")) {  
           flv.setText(Integer.parseInt(flv.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("avi")) {  
           avi.setText(Integer.parseInt(avi.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("exe")) {  
           exe.setText(Integer.parseInt(exe.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("sh")) {  
           sh.setText(Integer.parseInt(sh.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("jar")) {  
           jar.setText(Integer.parseInt(jar.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("html")) {  
           html.setText(Integer.parseInt(html.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("php")) {  
           php.setText(Integer.parseInt(php.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("css")) {  
           css.setText(Integer.parseInt(css.getText()) + 1 + "");  
         } else if (ex.equalsIgnoreCase("js")) {  
           js.setText(Integer.parseInt(js.getText()) + 1 + "");  
         }  
       } else if (listFile.isDirectory()) {  
         try {  
           processDirectory(listFile.getAbsolutePath());  
         } catch (Exception e) {  
           System.err.println("Exception - " + e.getMessage());  
         }  
       }  
     }  
   } 

Comments

51 responses to “Directory / Folder Indexing Program Using Java”

  1. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  2. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  3. … [Trackback]

    […] Here you can find 77254 more Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  4. … [Trackback]

    […] Here you can find 78232 additional Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  5. … [Trackback]

    […] Information to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  6. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  7. … [Trackback]

    […] There you will find 37337 additional Info to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  8. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  9. … [Trackback]

    […] There you will find 75706 more Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  10. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  11. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  12. … [Trackback]

    […] Here you will find 7871 more Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  13. … [Trackback]

    […] There you can find 93403 additional Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  14. … [Trackback]

    […] Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  15. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  16. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  17. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  18. … [Trackback]

    […] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  19. … [Trackback]

    […] Information to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  20. … [Trackback]

    […] Here you can find 36473 additional Info to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  21. … [Trackback]

    […] Here you can find 10763 additional Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  22. … [Trackback]

    […] There you can find 86807 more Info to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  23. … [Trackback]

    […] Here you will find 8516 more Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  24. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  25. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  26. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  27. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  28. … [Trackback]

    […] There you will find 71865 more Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  29. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  30. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  31. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  32. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  33. … [Trackback]

    […] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  34. … [Trackback]

    […] There you will find 51504 more Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  35. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  36. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  37. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html-2/ […]

  38. get enclomiphene price singapore

    cheap enclomiphene generic pharmacy usa

  39. sans ordonnance kamagra pharmacie gratuit comprime acheter en ligne

    generique kamagra pilule du lendemain

  40. get androxal price prescription

    cheap androxal without prescription

  41. buy dutasteride canada online order

    no prescription next day delivery dutasteride

  42. get flexeril cyclobenzaprine online mastercard accepted

    online order flexeril cyclobenzaprine no prescription mastercard

  43. ordering gabapentin generic cheapest

    discount gabapentin generic how effective

  44. cheap fildena uk meds

    comprar fildena generica

  45. discount itraconazole without rx online

    order itraconazole uk how to get

  46. ordering staxyn american express

    discount staxyn canada price

  47. how to buy avodart purchase online canada

    purchase avodart purchase tablets

  48. discount rifaximin generic best price

    purchase rifaximin generic low price

  49. buying xifaxan generic london

    xifaxan no prior script

  50. koupit kamagra online z kanady

    pナ册dpis kamagra cod

Leave a Reply