REBOL [
    Title: "Function to get all names of certain file types"
    Purpose: {Generalized version of a function from a program by
    Carl.  Given a specified folder and a block of file types, 
    return a block of all the file names in the specified 
    directory that are of that type.}
]

;; [---------------------------------------------------------------------------]
;; [ This is a function borrowed from Carl's picture viewer program.           ]
;; [ Pass to it a folder name and a block of file types, the program will      ]
;; [ get a list of files in the specified folder that are of the specified     ]
;; [ types.  For example:                                                      ]
;; [     FILE-LIST: FILES-OF-TYPE-IN-DIR (fodername) [%.jpg %.JPG %.png %.PNG] ]
;; [ Note how you can make a function within a function.                       ]
;; [ General procedure is to get a list of all files in the folder.            ]
;; [ Go through the list.  If a file has the desired suffix, go on to the      ]
;; [ next file.  Otherwise remove it from the list.  At the end, reposition    ]
;; [ to the head of the list and return it to the caller.                      ]
;; [---------------------------------------------------------------------------]

FILES-OF-TYPE-IN-DIR: func [
    SOURCE-FOLDER
    TYPE-LIST
    /local FILE-ID-LIST
] [
    OF-TYPE?: func [
        FILE-ID 
    ] [
        find TYPE-LIST find/last FILE-ID "."
    ]
    FILE-ID-LIST: copy []
    FILE-ID-LIST: read SOURCE-FOLDER/.
    while [not tail? FILE-ID-LIST] [
        either OF-TYPE? first FILE-ID-LIST [
            FILE-ID-LIST: next FILE-ID-LIST
        ] [
            remove FILE-ID-LIST
        ]
    ]
    FILE-ID-LIST: head FILE-ID-LIST
    return FILE-ID-LIST
]

;;Uncomment to test
;TEST-DIR: request-dir
;TEST-LIST: FILES-OF-TYPE-IN-DIR TEST-DIR [%.pdf]
;probe TEST-LIST
;halt