Example

Get a Directory File List

Author: Carl Sassenrath
Return to REBOL Cookbook

This example reads a directory and returns all the file and directory names within it.


    files: read %docs/

To see the list, just add a PROBE:


    probe files

If you want the file list in sorted order:


    files: sort read %docs/
    probe files

To read the current directory:


    files: read %./
    probe files

(The dot is a shortcut for the current directory.)

You can remove any unwanted filenames from the list with this code:


    files: read %docs/
    remove-each file files [
        not find file "web"
    ]
    probe files

Here the REMOVE-EACH function removes all file names that do not contain the string "web". The FIND function can include patterns (/any) and matches (/match) as well.

If the directory has subdirectories, you can remove them from the list with:


    files: read %docs/
    remove-each file files [
        find file "/"
    ]
    probe files

Here's how you combine the above to get file names that contain the string "web" and not directories:


    files: read %docs/
    remove-each file files [
        any [
            not find file "web"
            find file "/"
        ]
    ]
    probe files

If you want files that have a specific set of suffixes:


    files: read %docs/
    remove-each file files [
        not find [%.gif %.jpg %.png] suffix? file
    ]
    probe files

Now the list only contains image file names.

Notes
  • File names that end with a slash (/) indicate directory names. They refer to a directory and not a plain file.
  • REMOVE-EACH and SUFFIX? are only in newer versions of REBOL.


2006 REBOL Technologies REBOL.com REBOL.net