REBOL Programming/list-dir

From Wikibooks, open books for an open world
< REBOL Programming
Jump to: navigation, search

USAGE:[edit]

LIST-DIR dir 

DESCRIPTION:[edit]

Prints a multi-column sorted listing of a directory.

LIST-DIR is a function value.

ARGUMENTS:[edit]

  • dir -- Directory to list or nothing (Type: file url unset)

(SPECIAL ATTRIBUTES)[edit]

  • catch

SOURCE CODE[edit]

list-dir: func [
    {Prints a multi-column sorted listing of a directory.} 
    [catch] 
    dir [file! url! unset!] "Directory to list or nothing" 
    /local file max s
][
    dir: throw-on-error [
        either value? 'dir [
            read dirize dir
        ] [
            read %.
        ]
    ] 
    dir: sort dir 
    max: 0 
    foreach file dir [
        if (length? file) > max [max: length? file]
    ] 
    max: max + 2 
    s: make string! 0 
    foreach file dir [
        append s file 
        while [((length? s) // max) <> 0] [append s " "] 
        if (length? s) > 60 [
            print s 
            clear s
        ]
    ] 
    if length? s [print s]
]