Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

43
LINES

< > BotCompany Repo | #744 // Find a file

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (63L/1K/3K).

1  
!636
2  
!modern
3  
4  
main {
5  
  static S programID;
6  
  static S searchFor;
7  
  static int count;
8  
  
9  
  psvm {
10  
    if (args.length == 0) {
11  
      print("I search for files under the current directory.");
12  
      print("Syntax: " + programID + " filetosearch");
13  
    }
14  
    searchFor = args[0];
15  
16  
    scan(".");
17  
    
18  
    if (count == 0) print("No results.");
19  
  }
20  
  
21  
  static void scan(S file) {
22  
    scan(new File(file));
23  
  }
24  
  
25  
  static S makeSearchPattern(S pat) {
26  
    return pat.replace(".", "\\.").replace("*", ".*"); // rough...
27  
  }
28  
  
29  
  static boolean match(S pat, File f) {
30  
    return Pattern.compile(makeSearchPattern(pat), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(f.getName()).matches();
31  
  }
32  
  
33  
  static void scan(File f) {
34  
    if (match(searchFor, f)) {
35  
      print("Found " + (f.isFile() ? "file" : "dir") + ": " + f.getPath());
36  
      ++count;
37  
    }
38  
    if (f.isDirectory()) {
39  
      File[] l = f.listFiles();
40  
      if (l != null) for (File x : l) scan(x);
41  
    }
42  
  }
43  
}

Author comment

Began life as a copy of #1000702

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #744
Snippet name: Find a file
Eternal ID of this version: #744/1
Text MD5: c6ace841109c099a607d67b5c92702e9
Transpilation MD5: 4e2b11a71d4e22349450b7affaa3c5bc
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-23 16:01:16
Source code size: 1014 bytes / 43 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 692 / 769
Referenced in: [show references]