8.2 Using the directory Fragment

A program using the directory fragment will have the following structure:

INCLUDE '~beta/basiclib/directory'
--- program: descriptor ---
(# ...
   dir: @directory;
do ...
   '/user/local/lib/beta/basiclib/' -> dir.name;
   dir.scanEntries(# do found.name -> putline #);
...
#)

8.2.1 Listing a directory

Program showing a simple use of directory: The directory with the path given as argument is scanned, and the names of all the entries are printed with an indication of what type of entry it is. This is done using the select pattern of scanentries. This is a more efficient strategy than using found.entry.isFile etc., possibly correcting for the case that 'd' is not current working directory.

If the path given is not a directory, an exception will be raised:

Program 10: listdir.bet

ORIGIN '~beta/basiclib/directory';

--- program: descriptor ---

(* Program showing a simple use of directory: The directory with the path 
 * given as argument is scanned, and the names of all the entries are printed
 * with an indication of what type of entry it is.
 * This is done using the 'select' pattern of 'scanentries'. This is a more
 * efficient startegy than using 'found.entry.isFile' etc., possibly 
 * correcting for the case that 'd' is not current working directory.
 * If the path given is not a directory, an exception will be raised.
 *)

(# arg: ^text; 
   d: @directory; 
   nl: @boolean; 
   full: @boolean;
   usage:
     (# do 'Usage: ' -> puttext; 
        1->arguments->puttext; 
        ' [-f] path' -> putline;
        stop;
     #);
do (* Parse command line *)
   (if noOfArguments
    // 1 then '.' -> d.name
    // 2 then
       2 -> arguments -> arg[];
       (if '-f' -> arg.equal then
           true -> full;
           '.' -> d.name;
        else
           arg[] -> d.name;
       if)
    // 3 then
       2 -> arguments -> arg[];
       (if '-f' -> arg.equal then
           true -> full;
        else usage;
       if);
       3 -> arguments -> d.name;
    else
       usage;
   if);
   
   (* Scan directory *)
   newline;
   'The content of \''-> puttext;
   d.name -> puttext;
   '\' is: ' -> putline;
   d.scanEntries
   (# 
   do select
      (# whenFile::<
           (# do 'File:      ' -> puttext; #);
         whenDir::<
           (# do 'Directory: ' -> puttext; #);
         whenOther::<
           (# do '(Unknown): ' -> puttext; #);
      #);
      (if full then foundFullPath -> putline; 
       else found.path -> putline; 
      if);
   #);
   newline;
#)


Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Friday November 10th 2000 at 14:53]