Classic Computer Magazine Archive COMPUTE! ISSUE 73 / JUNE 1986 / PAGE 11

Readers Feedback

The Editors and Readers of COMPUTE!

If you have any questions, comments, or suggestions you would like to see addressed in this column, write to "Readers' Feedback," COMPUTE!, P.O. Box 5406, Greensboro, NC 27403. Due to the volume of mail we receive, we regret that we cannot provide personal answers to technical questions.

IBM BASIC Directory
Can you tell me how to read and display the disk directory on an IBM PC from within a BASIC program?

Kamal Ashour

 There are two simple ways to approach this. The first is simply to print the directory to the screen at the appropriate time in your BASIC program. A second method would be to read the directory into a string array for use by your program at some later point. Here's a short routine that employs the first method:

EG 100 REM FSPEC$="A:*.*":GOTO 1
       40
MD 110 PRINT:PRINT"Select drive:
        (";:COLOR 16,15:PRINT"A
       B";:COLOR 7,0:PRINT CHR$(
       29)CHR$(29)"/"CHR$(28)")"
HB 120 DRIVE$=INKEY$+":":A=ASC(D
       RIVE$):IF (A OR 32)<97 OR
       (A OR 32)>98 THEN 120
IF 130 DRIVE$=CHRS(A AND 223)+":
       ":FSPEC$=DRIVE$+"*.*"
GG 140 ON ERROR GOTO 150:FILES F
       SPEC$:ON ERROR GOTO 0:END
JL 150 BEEP:COLOR 31:CLS:PRINT "
       Cannot read directory":CO
       LOR 7:ON ERROR GOTO 0:END

    This routine will ask you from which drive (A: or B:) you want to read the directory. If you have a single-drive system (drive A: only), remove the REM from line 100. Here's another routine that uses the second method:

KJ 1000 REM FSPEC$="A:*.*":GOTO
        1040
LH 1010 PRINT:PRINT"Select drive
        : (";:COLOR 16,15:PRINT"
        A B";:COLOR 7,0:PRINT CH
        R$(29)CHR$(29)"/"CHR$(28
        )")"
HC 1020 DRIVE$=INKEY$+":":A=ASC(
        DRIVE$):IF (A OR 32)<97
        OR (A OR 32)>98 THEN 102
        0
ME 1030 DRIVE$=CHR$(A AND 223)+"
        :":FSPEC$-DRIVE$+"*.*"
LN 1040 DEF SEG=0:WIDTH 80
IF 1050 HEAD=1050:TAIL=1052:BUFF
        ER=1054
OJ 1060 CLS:COLOR 23,0,0:PRINT"R
        eadinq disk directory"
OE 1070 COLOR 0:ON ERROR GOTO 10
        90
EJ 1080 FILES FSPEC$:ON ERROR GO
        TO 0:GOTO 1100
IL 1090 BEEP:COLOR 31:CLS:PRINT
        "Cannot read directory":
        COLOR 7:ON ERROR GOTO 0:
        END
HF 1100 DIM TT$(24):LOCATE 3,1:C
        OLOR 7:ROWS=0
CD 1110 POKE HEAD,30:POKE TAIL,3
        4:POKE BUFFER,0:POKE BUF
        FER+1,79:POKE BUFFER+2,1
        3:POKE BUFFER+3,28:'Put
        code for End, Enter into
         keyboard
DE 1120 LINE INPUT TT$(ROWS):IF
        TT$(ROWS)<>"" THEN ROWS
        ROWS+1:GOTO 1110
DN 1130 IF NOT DIMMED THEN DIM F
        $(ROWS*4-1):DIMMED-1
JP 1140 ROWS=ROWS-1:FOR I=0 TO R
        OWS:FOR J=0 TO 3
KE 1150 T$=MID$(TT$(I),J*18+1,12
        )
KD 1160 IF T$<>"" THEN F$(ENTRIE
        S)=T$:ENTRIES=ENTRIES+1
KA 1170 NEXT J:NEXT I:ERASE TT$:
        ENTRIES=ENTRIES-1:DEF SE
        G:RETURN


    This routine reads the filenames from the disk directory into an array named F$. One advantage of this method is that you need to look only once at the directory. Once the directory information is stored in a string, you can extract the filenames whenever it's convenient and print them in any format you like. With a little more programming, you could cursor through the directory to access a particular file, sort the directory entries alphabetically, catalog all your disks, or whatever. Again, remove the REM from line 1000 if you have a single-drive system.