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).

!636
!modern

main {
  static S programID;
  static S searchFor;
  static int count;
  
  psvm {
    if (args.length == 0) {
      print("I search for files under the current directory.");
      print("Syntax: " + programID + " filetosearch");
    }
    searchFor = args[0];

    scan(".");
    
    if (count == 0) print("No results.");
  }
  
  static void scan(S file) {
    scan(new File(file));
  }
  
  static S makeSearchPattern(S pat) {
    return pat.replace(".", "\\.").replace("*", ".*"); // rough...
  }
  
  static boolean match(S pat, File f) {
    return Pattern.compile(makeSearchPattern(pat), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(f.getName()).matches();
  }
  
  static void scan(File f) {
    if (match(searchFor, f)) {
      print("Found " + (f.isFile() ? "file" : "dir") + ": " + f.getPath());
      ++count;
    }
    if (f.isDirectory()) {
      File[] l = f.listFiles();
      if (l != null) for (File x : l) scan(x);
    }
  }
}

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: 691 / 767
Referenced in: [show references]