File Utilities

exists(String fileName ) or
(File file )

exists() returns if the specified file exists or not.

ls()
ls(String directory)

ls() displays the files in the specified directory. If directory is not specified, the directory ${user.dir} is implicitly used.

chdir(String directory)

chdir() changes the current directory (the property "user.dir") to the specified directory.

pwd()

pwd() displays the current directory (the property "user.dir").

mkdir(String directory) or
(File directory)

mkdir() makes a directory.

renameTo(String old , String new) or
(File old , File new)

renameTo() changes the name of the file old to new.

delete(String file {, boolean recursive }) or
(File file {, boolean recursive })

delete() deletes the specified file. If the file is a directory and it is not empty, the directory is deleted only when recursive is true.

find(String dir {, PnutsFunction action {, Object extra, PnutsFunction recalc } }) or
(File dir {, PnutsFunction action {, Object extra, PnutsFunction recalc } })

find() traverses a directory tree recursively. If action is specified, it should return a boolean value. action is applied to each file (and directory) under the directory with an extra argument, as long as it returns true.

action(File file, Object extra)

For example, the following example shows how to delete "*~" files under the current directory.

find(".", function (f, a) {if (f.getName().endsWith("~")) delete(f); true})

If action is not specified, each file is printed on the output stream of the current context.

When recalc is specified and non-null, the extra argument is re-calculated by the function, applying to each file and the argument extra.

extra = recalc(File file, Object extra)

For example, the following example prints file names with indents to show the structure of the directory.

function printTree(base){
   find(base,
        function (f, extra) {println(extra + f.getName()); true},
        "",
        function (f, extra) extra + "  ")
}
copy(String source , String destination ) or
(File source , File destination )

If source is a directory, copy() copies all files under the source directory to destination directory. If source is a ordinary file and destination is a directory, copy() copies the file to destination directory. If both source and destination are ordinary files, copy() copies the content of the source file to the destination file.

cat(String file {, OutputStream output } ) or
(Object[] files {, OutputStream output })

cat() displays the content of file or files. If output is specified, the content is written to the output.

zcat(String fileName {, OutputStream output }) or
(InputStream input {, OutputStream output })

zcat() decompress the content of file and displays. If outputStream is specified, data is written to the stream. Otherwise, it returns a java.io.PipedInputStream object which can be read with read() function.

The following example illustrates how to load a gzip-compressed script.

load(zcat("big.pnut.gz"))
gzip(String fileName {, OutputStream output }) or
(InputStream input {, OutputStream output })

gzip() compress the content of fileName or input . If output is specified, data is written into the stream. Otherwise, it returns a java.io.PipedInputStream object which can be read with read() function.

grep(String pattern , String filePattern) or
(String pattern , Enumeration files)
(String pattern , Object[] files)

grep() searches the regular expression pattern from the specified files or files whose names matche the filePattern, and displays the matched lines. PerlTools1.2 or higher of ORO software is used in this function.

grep("println", "*/*/*pnut")

Back