import java.io.*;
import java.util.regex.*;
public class DirFilter{
public static void main(String args[])
{
// No error checking anywhere in this code, place your own
String filterArg = args[0];
// Replace * with .* as requtested in Regular Expression
// Replace . with \. as requtested in Regular Expression
Pattern pn = Pattern.compile("\\.");
filterArg = pn.matcher(filterArg).replaceAll("\\\\.");
pn = Pattern.compile("\\*");
filterArg = pn.matcher(filterArg).replaceAll("\\.\\*");
// System.out.println(filterArg);
// Search against the current directory (Unix way of directory for compatibility)
File theDir = new File("./.");
String fileList[] = theDir.list();
pn = Pattern.compile(filterArg, Pattern.CASE_INSENSITIVE);
Matcher mt = pn.matcher(fileList[0]);
if(mt.find())
{
System.out.println(fileList[0]);
}
for(int i = 1; i<fileList.length; i++)
{
System.out.println(fileList[i]);
mt = mt.reset(fileList[i]);
if(mt.find())
{
System.out.println(fileList[i]);
}
}
}
}
You have to compile and run it under JDK 1.4 or later as it utilizes Regular Expression.
Not optimized. No error checking. But it compiles and works. Check it out.