-4

I'm writing a program that does various data analysis functions for use with Excel.

I need a way of returning file names of documents so I can search through them and find the ones I want.

I need to be able to take a string, saved as a variable, and use it to return the name of every document in a folder whose file name contains that string.

This will be used to sift through pre-categorized sections of data. Ideally I would save those documents' file names in a string array for later use within other functions.

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
Kael Eppcohen
  • 177
  • 1
  • 2
  • 10
  • What do you mean by `titles` ? Do you mean file names ? And why do you want to store the names of the files ? – SomeDude Aug 19 '16 at 18:07
  • 2
    *How would I get started writing the search portion of this program?* - Research. – Jonny Henly Aug 19 '16 at 18:08
  • Does the user specify the directory to search for? – Orin Aug 19 '16 at 18:10
  • 2
    Stack Overflow is really not meant for others to do the work and guide you/give you a solution. The intent is that you try something on your own first, and then if you're having a problem with your implementation then you bring that here. Please show some initial effort. Thank you. – Andrew Aug 19 '16 at 18:11
  • Your question only contains requirements - it is not showing any efforts from your side to solve this problem yourself. Please add your attempts to this questions - as this site is not a free "we do your (home)work" service. Beyond that: please turn to the [help] to learn how/what to ask here. Thanks! – GhostCat Aug 08 '17 at 12:21

3 Answers3

2
private List<String> searchForFileNameContainingSubstring( String substring )
{
   //This is assuming you pass in the substring from input.
   File file = new File("C:/Users/example/Desktop"); //Change this to the directory you want to search in.

   List<String> filesContainingSubstring = new ArrayList<String>();

   if( file.exists() && file.isDirectory() )
   {
       String[] files = file.list(); //get the files in String format.
       for( String fileName : files )
       {
           if( fileName.contains( substring ) ) 
                filesContainingSubstring.add( fileName );
       }
   }

   for( String fileName : filesContainingSubstring )
   {
      System.out.println( fileName ); //or do other operation 
   }

   return filesContainingSubstring; //return the list of filenames containing substring.
}

Using this method, you could pass in the input from the user as the string you want the filename to contain. The only other thing you need to change is where you want in your directory to start searching for files, and this program only looks in that directory.

You could further look recursively within other directories from the starting point, but I won't add that functionality here. You should definitely look into it though.

This also assumes that you are looking for everything within the directory, including other folders and not just files.

Orin
  • 920
  • 5
  • 16
1

You can get the list of all the files in a directory and then store them in an array. Next, using the java.io.File.getName() method, you can get the names of the files. Now you can simply use the .indexOf() method to check whether the string is a substring of the file name. I assume that all the items in the directory of concern are files and not sub directories.

public static void main(String[] args) throws IOException {
    File[] files = new File("X:/").listFiles(); //X is the directory
    String s <--- the string you want to check filenames with
    for(File f : files){
        if(f.getName().toLowerCase().indexOf(s.toLowerCase()) != -1)
        System.out.println(f.getName());
    }
}    

This should display the names of all those files in the directory X:\ whose names include the String s.


References

  1. This question: How do I iterate through the files in a directory in Java?

  2. The java.io.File.getName() method


Statutory edit info

I have edited this answer simply to replace the previous algorithm, for checking the existence of a substring in a string, with the one that is currently used in the code above.

Community
  • 1
  • 1
progyammer
  • 1,498
  • 3
  • 17
  • 29
1

Here is an answer to search the file recursively??

String name; //to hold the search file name

public String listFolder(File dir) {
    int flag;
    File[] subDirs = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    });
    System.out.println("File of Directory: " + dir.getAbsolutePath());
    flag = Listfile(dir);
    if (flag == 0) {
        System.out.println("File Found in THe Directory: " + dir.getAbsolutePath());
        Speak("File Found in THe Directory: !!" + dir.getAbsolutePath());
        return dir.getAbsolutePath();
    }
    for (File folder : subDirs) {
        listFolder(folder);
    }
    return null;
}

private int Listfile(File dir) {
    boolean ch = false;
    File[] files = dir.listFiles();
    for (File file : files) {
        Listfile(file);
        if (file.getName().indexOf(name.toLowerCase()) != -1) {//check all in lower case
            System.out.println(name + "Found Sucessfully!!");
            ch = true;

        }
    }
    if (ch) {
        return 1;
    } else {
        return 0;
    }
}
susan097
  • 3,500
  • 1
  • 23
  • 30
  • It is a simple, as name refers the file name you want to search! And another thing to point out that you should give directory name as "C://" ,but it is slow to search the file onto the whole directory.So, you should also give the folder name as "C://fold_name" and it is fast now. Any other explanation you require?? Please check in your IDE – susan097 Aug 08 '17 at 12:25
  • Dont point out these things in comments. Consider editing your answer instead! – GhostCat Aug 08 '17 at 12:30