comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fileIO/FindFiles.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package fileIO;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6
7 public class FindFiles {
8
9
10 public static void main(String[] args){
11
12 String root=args[0];
13 // if(root.equals(".")){root=null;}
14 String prefix=args[1];
15 String suffix=(args[2].equals("null") ? null : args[2]);
16 String middle=null;
17
18 if(args.length>3){
19 middle=(args[3].equals("null") ? null : args[3]);
20 }
21
22 boolean NEWLINE=true;
23 boolean BOTH=true;
24
25 ArrayList<String> results=findFiles(root, prefix, suffix, middle);
26 for(String s : results){
27 if(NEWLINE){
28 System.out.println(s);
29 }else{
30 System.out.print(s+" ");
31 }
32 }
33
34
35 if(BOTH){
36 System.out.println();
37 NEWLINE=!NEWLINE;
38 for(String s : results){
39 if(NEWLINE){
40 System.out.println(s);
41 }else{
42 System.out.print(s+" ");
43 }
44 }
45 }
46 }
47
48
49 public FindFiles(String pre, String suf, String mid){
50 assert(!"*".equals(pre)) : "Use # instead of *, which has problems from the command line";
51 assert(!"*".equals(suf)) : "Use # instead of *, which has problems from the command line";
52 prefix=((pre==null || pre.equals("*") || pre.equals("#")) ? null : pre.toLowerCase());
53 suffix=((suf==null || suf.equals("*") || suf.equals("#")) ? null : suf.toLowerCase());
54 middle=((mid==null || mid.equals("*") || mid.equals("#")) ? null : mid.toLowerCase());
55 }
56
57 public static ArrayList<String> findFiles(String root, String prefix, String suffix){
58 return findFiles(root, prefix, suffix, null);
59 }
60
61 public static ArrayList<String> findFiles(String root, String prefix, String suffix, String mid){
62 FindFiles ff=new FindFiles(prefix, suffix, mid);
63 return ff.findFiles(root);
64 }
65
66 public ArrayList<String> findFiles(String path){
67 findFiles(new File(path==null ? "." : path));
68 return results;
69 }
70
71 public ArrayList<String> findFiles(File path){
72
73 if(path.isDirectory()){
74 File[] array=path.listFiles();
75 if(array==null){System.err.println("null contents for "+path.getAbsolutePath());}
76 else{for(File f : array){findFiles(f);}}
77 }else{
78 consider(path);
79 }
80 return results;
81 }
82
83 public void consider(File in){
84 // System.out.println("Considering "+in.getAbsolutePath()+" versus '"+prefix+"' '"+suffix+"'");
85 if(!in.exists()){return;}
86 assert(in.exists()) : in;
87 assert(in.isFile());
88 String abs=in.getAbsolutePath();
89 // System.out.println("Considering "+abs);
90 String abs2=abs.toLowerCase();
91 int slashLoc=abs2.lastIndexOf(slash);
92 if(slashLoc>-1){
93 abs2=abs2.substring(slashLoc+1);
94 }
95 // System.out.println("a");
96 if(prefix!=null && !abs2.startsWith(prefix)){return;}
97 // System.out.println("b");
98 if(suffix!=null && !abs2.endsWith(suffix)){return;}
99 // System.out.println("c");
100
101 if(middle!=null && !abs2.contains(middle)){return;}
102
103 results.add(abs);
104 }
105
106
107 public ArrayList<String> results=new ArrayList<String>();
108 public String prefix;
109 public String suffix;
110 public String middle;
111 public static final char slash=System.getProperty("file.separator").charAt(0);
112
113 }