java - get file list from glob without specifying base directory -


there previous questions checking if file matches glob pattern (here one). however, list of files match glob pattern without having specify base directory search. need accept both relative , absolute directories (i resolve relative ones specified directory), , needs cross-platform compatible.

given string such "c:/users/foo/", "/user/foo/.txt" or "dir/*.txt", how list of matching paths?

yes, you'll need programmatic way find out if glob pattern absolute. can done follows:

    (string glob : new string[] { "../path/*.txt", "c:/../path/*.txt", "/../path/*.txt" }) {         system.out.println(glob + ": " + (new file(glob).isabsolute() ? "absolute" : "relative"));     } 

on windows output

../path/*.txt: relative c:/../path/*.txt: absolute /../path/*.txt: relative 

on unix last absolute. if know glob pattern relative, prepend special directory it. after you'll have absolute path glob patterns , can use specify search.

edit 1 per comment, can following. can mix , match nio , io. should know java.io.file.isabsolute() checks file path format, not if file exists, determine if it's in absolute or relative form. in platform specific manor.

    string basedir = "c:/basedir/";     (string glob : new string[] { "../path/*.txt", "c:/../path/*.txt", "/../path/*.txt" }) {         file file = new file(glob);         if (!file.isabsolute()) {             file = new file(basedir, glob);         }         system.out.println(file.getpath() + ": " + (file.isabsolute() ? "absolute" : "relative"));     } 

this print

c:\basedir\..\path\*.txt: absolute c:\..\path\*.txt: absolute c:\basedir\..\path\*.txt: absolute 

you still have globbing or use methods described in post mentioned (how find files match wildcard string in java?)


Comments

Popular posts from this blog

Multilayer CSV to filtered excel -

Listboxes in c# -

ios - Why must I define variables twice in the Header file? -