Recursive Directory 1/

 BBS: Inland Empire Archive
Date: 04-14-92 (16:46)             Number: 78
From: MICHAEL MALLEY               Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: Recursive Directory   1/       Conf: (2) Quik_Bas
Hello everybody!

This is my entry in the utility of the month contest [Grin].  BTREE
displays a graphical representation of a disk's directory structure with
an optional files listing much like DOS 5's TREE.COM, however it is not
hindered by the same limitations.  TREE.COM will insert extra carraige
returns and really junk up your screen with a nesting of directories
> 19 or so, BTREE does not (yeah David, I know... "only nerds would have
that" [Grin]).

This source code will be helpful to all who are trying to learn a little
more about writing recursive programs, and those that want a really good
file and directory routine.  The most efficient way to do this in terms
of memory is *not* recursion, however since it is only a command line
utility, and speed of development was what was important, I choose to do
it this way.

The code is pretty much self documenting, but there are a few points
worth mentioning.  First of all all output is redirectable!  The code
uses no LOCATEs in its work.  This means that you can type:
  BTREE C:\ /F > PRN
and have a directory tree with files coming out of your printer.  This
is where the problem comes in.  There is NO error trapping, and on my
end, if you turn off the printer while redirection is in effect, the
computer locks up.  This makes no sence to me what-so-ever, as if there
is no error trapping, the program *should* fail and drop back to the DOS
prompt.  Oh well, it just makes life exciting when it doesn't follow the
manuals [Grin].  Also, during the DOS interrupts to get a file/directory
name, if you specified a drive that has no disk in in, the program will
stop with an Error xxx in module BTREE.  Funny how it flip-flops on what
it will do huh?  If this is important to you, you can write an error
trapping routine.  To stop the program, you can press <CTRL>+C.  Lastly,
ALL files will be shown, hidden and system included.  TREE.COM will not
show these.

I wrote this and was able to compile it to 18,354 bytes using PDS 7.1,
and it was tested on my hard disk and a floppy with 32 levels of
directory nesting.  One of you guys with PDQ pop up and let me know how
tiny you could make it.  Compilation should be done with string
compaction OFF, otherwise the EXE will be larger.  There is one
underscore (_) in the declaration for InterruptX.  This should be
deleted and the following line appended.  There are three places where
QB users must change the source, but these are marked and the correct
changes given.  Have fun and somebody PLEASE let me know if it made it
out there.

Thanks - Michael  :)

 ---------------------------[ BTREE.BAS ]------------------------------
'Title:   BTREE2.BAS
'Purpose: Shows a graphical representation of the directory structure.
'By:      Michael Malley
'Written: April 14, 1992
'
'Note:  Redirection to devices is enabled.
'
'Released into the public domain as long as the intial screen is kept
'intact.  Use of the algorithms in other applications may be used without
'acknowledgment as long as that application is sufficiently different in
'purpose than BTREE.

DECLARE SUB SetDTA (Segment%, Offset%)
DECLARE SUB ShowTree (PathSpec$, Position AS INTEGER)
DECLARE FUNCTION GetFile$ (Spec$, DTA AS ANY, FileNames AS INTEGER)

DEFINT A-Z

TYPE RegtypeX
    ax    AS INTEGER
    bx    AS INTEGER
    cx    AS INTEGER
    dx    AS INTEGER
    bp    AS INTEGER
    si    AS INTEGER
    di    AS INTEGER
    flags AS INTEGER
    ds    AS INTEGER
    es    AS INTEGER
END TYPE
DECLARE SUB InterruptX (intnum AS INTEGER, Inreg AS RegtypeX, Outreg _
AS RegtypeX)

TYPE DTAType
    Junk  AS STRING * 21
    Attr  AS STRING * 1
    Time  AS INTEGER
    Date  AS INTEGER
    Size  AS LONG
    Name  AS STRING * 13
END TYPE

CONST False = 0
CONST True = NOT False

'--- Give enough of a stack for the recursion for all but the most trashed
'    directories
STACK 6144

DIM SHARED Regs AS RegtypeX, Levels AS STRING * 32, ScratchDTA AS DTAType
DIM SHARED ErrorLevel AS INTEGER, ShowFiles, KeyStroke AS STRING
DIM Spec AS STRING

'--- Levels:      a string of flags 32 characters long.  Used to allow child
'                 occurences of ShowTree to know what graphic lines must be
'                 drawn.  Using a string results in a savings of memory of
'                 32 bytes and faster execution then when we use an array.
'
'    ErrorLevel:  Used internally to pass DOS errors while getting
'                 directory information.
'
'    Spec:        The initial pathspec to be used in the directory search.
'
'    ScratchDTA:  DTA used to retrieve filenames.
'
'    ShowFiles:   Flag to determine if we also show filenames.

Spec = RTRIM$(COMMAND$)

IF LEN(Spec) THEN
    Position = INSTR(Spec, "/F")
    IF Position THEN
        ShowFiles = True
        IF Position = 1 THEN
            Spec = LTRIM$(RIGHT$(Spec, LEN(Spec) - (INSTR(Spec, "/F") + 1)))
        ELSE
            Spec = RTRIM$(LEFT$(Spec, Position - 1))
        END IF
    END IF
END IF

IF LEN(Spec) = 0 THEN Spec = CURDIR$    '--- QB users use "." for Spec

PRINT "BTREE ver 1.0 Copyright(C) 1992 - Use /F for file listing."
PRINT "By: Michael S. Malley and released into the public domain."
PRINT
PRINT Spec

'--- Chop off any trailing \
IF ASC(RIGHT$(Spec, 1)) = 92 THEN Spec = LEFT$(Spec, LEN(Spec) - 1)

>>> Continued to next message

 * SLMR 2.1a * He's got a magnet!  Everybody BACKUP!!! - Cmdr. Data

--- Maximus 2.01wb
 * Origin: UltraTech - Nashville, TN  (615) 356-0453 {HST} (1:116/30)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET