2

Suppose I have files like these in a folder:

  • myfile_mark_1.mp4
  • myfile_john_2.mp4
  • myfile_jake_3.mp4
  • myfile_tristan_4.mp4
  • yourfile_mark_1.mp4

How do I find files where only the names containing "myfile_" in the prefix and suffix would be 3 or less("_3.mp4", "_2.mp4", "_1.mp4", something like this).

So in my program I could get:

  • myfile_mark_1.mp4
  • myfile_john_2.mp4 and
  • myfile_jake_3.mp4

I have tried this one, hoping to get lucky but I didn't get any result. :D

String myDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(myDirectory);
if (f.exists() && f.isDirectory()){
    final Pattern p = Pattern.compile("myfile_*\\_(^0*(1?\\d|%d)$).mp4"); // I know I really have a stupid mistake on the regex;

    File[] flists = f.listFiles(new FileFilter(){
        @Override
        public boolean accept(File file) {
            return p.matcher(file.getName()).matches();
        }
    });

    String s = "wait a minute, i'm debugging";
}

I hope someone can help out on my problem.

2
  • Please post your myDirectory path Commented Aug 12, 2013 at 5:35
  • Hi Yogesh, please see my edit.. I just put the files in the sd card without any sub folders just for testing. thanks Commented Aug 12, 2013 at 5:40

4 Answers 4

6

The regex required is quite simple.

myfile_.*_[123]\\.mp4
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the straightforward answer..Just what I needed :)
5

try this :-

 String myDirectory = Environment
        .getExternalStorageDirectory().getAbsolutePath();
 File f = new File(myDirectory);
 if (f.exists() && f.isDirectory()) {
    final Pattern p = Pattern.compile("yourfile_.*_[123]\\.png"); 
 File[] flists = f.listFiles(new FileFilter() {
    @Override
    public boolean accept(File file) {
        p.matcher(file.getName()).matches();
        Log.e("MIS", "sdsdsdsds" + p.matcher(file.getName()).matches());

        return p.matcher(file.getName()).matches();

    }
});
String s = "wait a minute, i'm debugging";

}

1 Comment

your answer and ravi's are same, but I think I have to give it to him since he answered first. +1 for my gratitude. Thanks!
1

If you want to do this with a Pattern, here's a working example, since the Pattern you use has many issues.

String mark = "myfile_mark_1.mp4";
String john = "myfile_john_2.mp4";
String jake = "myfile_jake_3.mp4";

Pattern pattern = Pattern.compile("myfile_\\p{Alpha}+_\\d+\\.mp4");
Matcher matcher = pattern.matcher(mark);
System.out.println(matcher.find());
matcher = pattern.matcher(john);
System.out.println(matcher.find());
matcher = pattern.matcher(jake);
System.out.println(matcher.find());

Output:

true
true
true

Issues I found in the Pattern you use:

  • I interpreted the ^0 part as an optional starting character. The ^ character can mean the beginning of the whole input, or the negation of a character or group (which only works in character classes).
  • The . in your Pattern must be escaped.
  • Not sure what the %d means in your Pattern, as it doesn't reflect anything in your examples. Is it because you want to group the numbers and back-reference them? In that case, you can wrap your numerical expression around round brackets - it'll be your index-1 group in this case.

Comments

0

Try some thing like this. I think it is easy

 String fileName="myfile_mark_1.mp4";
    if(fileName.startsWith("myfile")){
       if(Integer.parseInt(fileName.split("_")[2].substring(0,1))<=3){
           System.out.println(fileName);
       }
    }

Following will return all files in your computer match with your criteria.

 public static void main(String[] args) {
    File[] paths = File.listRoots();
    for(File directory:paths){
        getFile(directory.toString());
    }
}

public static void getFile(String directoryName) {
    File directory = new File(directoryName);
    File[] fList = directory.listFiles();
    if(fList!=null){
        for (File file : fList) {
            if (file.isFile()) {
                if(file.getName().toString().startsWith("myfile")){
                    if(Integer.parseInt(file.getName().toString().split("_")[2].substring(0,1))<=3){
                        System.out.println(file.getName().toString());
                    }
                }
            } else if (file.isDirectory()) {
                getFile(file.getAbsolutePath());
            }
        }
    }

}

1 Comment

yes, I could do like that one, but the files could be more than a hundred in the long run and I don't think looping through the whole files to find my files is good. But I am greatly considering your answer. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.