Example

Match File Extensions

Author: Anton Rolls
Return to REBOL Cookbook

In newer versions of REBOL you can get the suffix of a file with the SUFFIX? function:


    probe suffix? %file.txt
    %.txt

But, earlier versions of REBOL such as View 1.2.1 and Core 2.5.0 did not have the SUFFIX? function. So, here is a method that works in all versions:


    %"" = find/last/tail file extension

Note here that the file must be of the file datatype.

Example usage:


    file: %picture.png
    extension: %.png
    %"" = find/last/tail file extension
    true

I arrived at this method after some experimentation. At first glance it doesn't look like the most optimal approach, but it works robustly.

You can make a function, if needed:


    is-extension?: func [file ext] [
        "" = form find/last/tail file ext
    ]

Note that the FORM function was added to allow this function to work for strings as well as files:


    is-extension? "install.log" %.log
    true
    is-extension? %install.log %.log
    true

2006 REBOL Technologies REBOL.com REBOL.net