QB 4.x/Basic 7.x Example to List Subdirectories in Given Path (83811)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1

This article was previously published under Q83811

SUMMARY

This article demonstrates how to obtain a list of all subdirectories for a given path using the CALL INTERRUPT and CALL INTERRUPTX routines. In this example, the Basic INTERRUPT routines invoke MS-DOS interrupt service calls (SetDTA, FindFirst, and FindNext) to retrieve all files in the specified directory. As each file is retrieved, its attributes are checked to determine if the file is a subdirectory. If the file is a subdirectory, then the directory name is displayed.

MORE INFORMATION

For a similar example that loads an MS-DOS directory listing into an array, query in the Microsoft Knowledge Base on the following words:

Example and Load and Directory and Listing and Array

To make this example work in Basic PDS 7.0 or 7.1, modify the FirstFile function in the example below as follows:

  1. Include the QBX.BI file instead of the QB.BI file.
  2. DIM inregs as RegTypeX and outregs as RegTypeX.
  3. Change CALL INTERRUPT to CALL INTERRUPTX.
  4. Add the following line between the assignment for INREGS.DX and the CALL INTERRUPTX:
          inregs.ds = SSEG(FileName$)
    						

Example

DECLARE SUB SetDTA (DTABuffer AS ANY)
DECLARE FUNCTION FindFirstFile& (path$)
DECLARE FUNCTION FindNextFile& ()

' $INCLUDE: 'qb.bi'        ' 1. Use QBX.BI for PDS

TYPE DTABufferType
  Reserved     AS STRING * 21
  Attributes   AS STRING * 1
  CreateTime   AS INTEGER
  Accessdate   AS INTEGER
  FileSize     AS LONG
  FileName     AS STRING * 13
END TYPE

DIM DTABuffer AS DTABufferType

CLS
path$ = "c:\"
CALL SetDTA(DTABuffer)                      'Set DTA
IF (FindFirstFile(path$) = 0) THEN
  DO
    IF DTABuffer.Attributes = CHR$(16) THEN 'If file is a directory
      PRINT RTRIM$(DTABuffer.FileName)
    END IF
    CALL SetDTA(DTABuffer)
  LOOP WHILE (FindNextFile = 0)
END IF
END

FUNCTION FindFirstFile& (path$)
     ' 2. Use RegTypeX below for PDS
        DIM inregs AS RegType, outregs AS RegType

        inregs.ax = &H4E00
        inregs.cx = 16          'include directories in search
        FileName$ = path$ + "*.*" + CHR$(0)
        inregs.dx = SADD(FileName$)
     ' 3. Add this for PDS: inregs.ds = SSEG(Filename$)
        CALL interrupt(&H21, inregs, outregs) '4. Use INTERRUPTX for PDS
        FindFirstFile& = (outregs.ax AND &HF)

END FUNCTION

FUNCTION FindNextFile&
        DIM inregs AS regtype, outregs AS regtype

        inregs.ax = &H4F00
        CALL interrupt(&H21, inregs, outregs)
        FindNextFile = (outregs.ax AND &HF)
END FUNCTION

SUB SetDTA (DTABuffer AS DTABufferType)
        DIM inregs AS RegTypeX, outregs AS RegTypeX

        DTABuffer.CreateTime = 0             ' Clear DTABuffer
        DTABuffer.Attributes = " "
        DTABuffer.Accessdate = 0
        DTABuffer.FileSize = 0
        DTABuffer.FileName = SPACE$(13)

        inregs.ax = &H1A00                   ' Set DTA address
        inregs.ds = VARSEG(DTABuffer)
        inregs.dx = VARPTR(DTABuffer)
        CALL interruptX(&H21, inregs, outregs)

END SUB
				

REFERENCES

"Microsoft MS-DOS Programmer's Reference," Microsoft Press, 1991

"Advanced MS-DOS Programming," (Second Edition) by Ray Duncan, Microsoft Press, 1988

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB83811