简体   繁体   中英

How to perform a glob search on a previous directory

I'm trying to write a java program which will take either the exact path or name of a file, or will take some form of pattern like a glob. The internals of the program will just be repeated for every file which is found by the filepath specifier.

Here's an example of some of the paths I would expect to be able to enter:

example.txt

Processes 'example.txt' in the local directory.

*.txt

Processes all text files of the local directory.

../*

Processes all text files in the previous directory.

It's this last one that I'm having a lot of trouble with. I can get the first two to work by using a PathMatcher, but I can't get it to emulate the ../ without scanning essentially the whole drive which the program is running in.

I'm having a ton of trouble getting pattern matching to work. I just want it to work in a similar fashion to grep, where I can either use it to specify an exact file, or a pattern of files.

Are there any examples of this being done?

Sure, I'll take the points :D Also, someone might Google this and will have an easier time using an "accepted" answer.

The shell does this for you, and most probably less error-prone than what you are trying to do manually.

private static void main(String[] files) {
    for (String file : files) {
        process(file);
    }
}

This will work on all systems I'm aware of for pretty much all globbing you'll ever come across.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM