How to search for . exe?

Asked

Viewed 112 times

3

How do I make a Java application that searches for a ". exe" inside a folder and inside that folder has subfolders. I want it to search for me even if I have subfolders. And it returns true or false.

For example:

C: Windows I want you to think cmd.exe
Although I know that CMD.exe stays inside System32. I want it to look inside the subfolders of C:/Windows/

Could someone help me?

1 answer

2


I recommend using recursion to do this, take a look at the code below that I removed from the site: http://www.mkyong.com/java/search-directories-recursively-for-file-in-java/

In the example, he is looking for post.php inside /Users/mkyong/websites and its subdirectories.

package com.mkyong;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileSearch {

  private String fileNameToSearch;
  private List<String> result = new ArrayList<String>();

  public String getFileNameToSearch() {
    return fileNameToSearch;
  }

  public void setFileNameToSearch(String fileNameToSearch) {
    this.fileNameToSearch = fileNameToSearch;
  }

  public List<String> getResult() {
    return result;
  }

  public static void main(String[] args) {

    FileSearch fileSearch = new FileSearch();

        //try different directory and filename :)
    fileSearch.searchDirectory(new File("/Users/mkyong/websites"), "post.php");

    int count = fileSearch.getResult().size();
    if(count ==0){
        System.out.println("\nNo result found!");
    }else{
        System.out.println("\nFound " + count + " result!\n");
        for (String matched : fileSearch.getResult()){
        System.out.println("Found : " + matched);
        }
    }
  }

  public void searchDirectory(File directory, String fileNameToSearch) {

    setFileNameToSearch(fileNameToSearch);

    if (directory.isDirectory()) {
        search(directory);
    } else {
        System.out.println(directory.getAbsoluteFile() + " is not a directory!");
    }

  }

  private void search(File file) {

    if (file.isDirectory()) {
      System.out.println("Searching directory ... " + file.getAbsoluteFile());

            //do you have permission to read this directory?    
        if (file.canRead()) {
        for (File temp : file.listFiles()) {
            if (temp.isDirectory()) {
            search(temp);
            } else {
            if (getFileNameToSearch().equals(temp.getName().toLowerCase())) {           
                result.add(temp.getAbsoluteFile().toString());
            }

        }
        }

     } else {
        System.out.println(file.getAbsoluteFile() + "Permission Denied");
     }
      }

      }

}

In the case shown by the site, the output was as follows:

Searching directory ... /Users/mkyong/websites
Searching directory ... /Users/mkyong/websites/wp-admin
Searching directory ... /Users/mkyong/websites/wp-admin/css
Searching directory ... /Users/mkyong/websites/wp-admin/images
Searching directory ... /Users/mkyong/websites/wp-admin/images/screenshots
Searching directory ... /Users/mkyong/websites/wp-admin/includes
Searching directory ... /Users/mkyong/websites/wp-admin/js
Searching directory ... /Users/mkyong/websites/wp-admin/maint
Searching directory ... /Users/mkyong/websites/wp-admin/network
Searching directory ... /Users/mkyong/websites/wp-admin/user
Searching directory ... /Users/mkyong/websites/wp-content
Searching directory ... /Users/mkyong/websites/wp-content/plugins
//long list, omitted.

Found 3 result!

Found : /Users/mkyong/websites/wp-admin/includes/post.php
Found : /Users/mkyong/websites/wp-admin/post.php
Found : /Users/mkyong/websites/wp-includes/post.php
  • I liked the search differently. I managed to change it and it’s the way I needed it. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.