glob.frink

Download or view glob.frink in plain text format


/** This demonstrates using Java 1.7's file "glob" matching from the
    java.nio.file package.  See the PathMatcher documentation for details on
    allowed glob syntax:
    https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-

    See:
    https://docs.oracle.com/javase/tutorial/essential/io/find.html
    https://docs.oracle.com/javase/8/docs/api/java/nio/file/PathMatcher.html

*/


/** Returns an array of java.io.File objects matching the pattern.  These can
    be passed to the read[file] or lines[file] or readLines[file] functions,
    for instance.  If the dir argument is the empty string (the default)
    or "." this will read from the current working directory.

    See:
    https://docs.oracle.com/javase/8/docs/api/java/io/File.html

    The following program lists the first 3 lines in each Frink file in the
    current directory:

    for f = filesGlob["*.frink"]
       println["\n---------------\n" + f.getName[] + "\n" + joinln[first[lines[f], 3]]]
*/

filesGlob[pattern, dir=""] :=
{
   ret = new array
   ds = filesGlobStream[pattern, dir]
   for path = ds
      ret.push[path.toFile[]]

   ds.close[]  // Recommended to close resources after iteration is complete
   
   return ret
}

/** Returns a java.nio.file.DirectoryStream<Path> of java.nio.file.Path objects
    matching the pattern.

    This can be iterated over with a Frink for loop, for instance.  It is
    recommended to call the .close[] method on the returned stream after
    iteration is completed to free any resources.

    https://docs.oracle.com/javase/8/docs/api/java/nio/file/DirectoryStream.html

    The following program lists the first 3 lines in each Frink file in the
    current directory:

    for p = filesGlobStream["*.frink"]
       println["\n---------------\n" + p.toString[] + "\n" + joinln[first[lines[p], 3]]]
*/

filesGlobStream[pattern, dir=""] :=
{
   path = callJava["java.nio.file.Paths", "get", [dir, []]]
   ds   = callJava["java.nio.file.Files", "newDirectoryStream", [path, pattern]]
   return ds
}


Download or view glob.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen, eliasen@mindspring.com