0

I am trying to create a program that returns the name of all video files and folders in a file. I already found the pattern for the video files and it works fine

String pattern = "(^\\w[ .A-z0-9_-]+(\\.mp4$|\\.avi$|\\.mkv$))";

But i'm stuck on how to read the folders. Everything I've tried has the pattern reading folders, but also other files as well. Since the folder name may have dots anywhere, its hard to isolate by extensions. Any ideas on how to read only folders?

4
  • I don't know if it's intentional but your A-z has an uppercase 'A' and a lowercase 'z' Commented Aug 28, 2012 at 3:11
  • 5
    You have to call the File.isDirectory() method. Commented Aug 28, 2012 at 3:12
  • or keep a list of known extensions and use a negative match on that (Gross, unmaintainable, and unreadable.) Commented Aug 28, 2012 at 3:18
  • Thanks, File.isDirectory() is what I needed Commented Aug 28, 2012 at 12:36

1 Answer 1

1

I used this code (with help from BalusC) for finding the filenames and their extensions.

If you have got the regex right then just plug it in here and add rest of the code as per your requirements.

public static void main(String[] args){
    File[] files = new File("temp").listFiles();
    showFiles(files);
}

public static void showFiles(File[] files) {
    for (File file : files) {
        if (file.isDirectory()) {
            System.out.println("Directory: " + file.getName());
            showFiles(file.listFiles()); 
        } else {
            System.out.println("File: " + file.getName());
            getFileNameAndSuffix(file);
        }
    }
}
public static void getFileNameAndSuffix(File file) 
{
    int index = file.getName().lastIndexOf('.');
    System.out.println(file.getName().substring(0, index));
    System.out.println(file.getName().substring(index));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, File.isDirectory() is what I needed

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.