Microsoft KB Archive/63233

COBOL Program to Find Subdirectories in OS/2 Protected Mode

PSS ID Number: Q63233 Article last modified on 06-22-1990

3.00 3.00a OS/2

Summary: Microsoft COBOL versions 3.00 and 3.00a protected mode programs can find all the subdirectories in the current directory by calling the OS/2 API functions DosFindFirst and DosFindNext. DosFindFirst finds the first occurrence of a file description in the current directory. DosFindNext finds the next occurrence of the same file description. DosFindNext can be called repeatedly until all the directory names have been found. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2.

More Information: DosFindFirst requires parameters of the types WORD, DWORD, and PTR WORD. DosFindNext requires a subset of these. For specifics on how to declare and pass these parameters, see the sample program below. Note that a WORD (2 bytes) in COBOL has a picture clause of 9(4) COMP-5 and a DWORD (double-word, or 8 bytes) has a picture clause of 9(8) COMP-5. Parameters prefixed by the word PTR must passed BY REFERENCE because the API function requires the address to the parameter. Those without PTR must be passed BY VALUE. The parameters must also be passed in the reverse order because COBOL’S calling convention is the reverse of the convention that the API functions use. DosFindFirst is used to find the first occurrence of the specified file in the current directory. If it is successful, DosFindNext can be called to find the next occurrence of the file. This call to DosFindNext can be repeated until it is unsuccessful. When either DosFindFirst or DosFindNext is unsuccessful, they place an error code in the special COBOL data name “RETURN-CODE”. This is the standard behavior of all OS/2 API functions when called from COBOL. In the sample program below, even though the search attribute (Attribute) is set to find files that are directories (bit 4 set), the program still checks if the attribute of the found file (FileAttribute) is 16. This is because DosFindFirst and DosFindNext always find files with an attribute of 0, regardless of what search attribute is specified. These are called “normal” files and they are the most common type; for example, COBOL.EXE, ANIMATE.EXE, and LINK.EXE are all normal files. It is also important to note the length of the data-item that receives the name of the found file (FileName, in the example below). It must be 13 bytes to hold a 12-character name (XXXXXXXX.XXX) that is terminated by a null character. For more information on calling API functions from COBOL 3.00 and 3.00a, see the file OS2API.DOC included with COBOL 3.00a. For more information on DosFindFirst and DosFindNext, see Pages 516-518 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The following program (DIRLIST.CBL) lists all the subdirectories of the current directory. Enter the following to compile and link the program in MS OS/2: pcobol dirlist; link /nop dirlist,,,pcobol doscalls;

Code Example
$SET ANS85 $SET VSC2 DATA DIVISION. WORKING-STORAGE SECTION. * Null-terminated pathname to search for (&quot;*.*&quot;). 01 Pathname           PIC X(4)         VALUE X&quot;2A2E2A00&quot;. * Default search handle. 01 Handle             PIC 9(4)  COMP-5 VALUE 1. * Search for files with directory attribute set. 01 Attribute          PIC 9(4)  COMP-5 VALUE 16. * Length of buffer is 36 bytes. 01 BufferLength       PIC 9(4)  COMP-5 VALUE 36. * Find one match at a time. 01 Matches            PIC 9(4)  COMP-5 VALUE 1. * This reserved item must be set to 0. 01 Reserved           PIC 9(8)  COMP-5 VALUE 0. 01 Buffer. 05 CreationDate    PIC 9(4)  COMP-5. 05 CreationTime    PIC 9(4)  COMP-5. 05 LastAccessDate  PIC 9(4)  COMP-5. 05 LastAccessTime  PIC 9(4)  COMP-5. 05 LastWriteDate   PIC 9(4)  COMP-5. 05 LastWriteTime   PIC 9(4)  COMP-5. 05 FileSize        PIC 9(8)  COMP-5. 05 Allocation      PIC 9(8)  COMP-5. *  Receives attribute of match; if 16, then it's a directory. 05 FileAttribute   PIC 9(4)  COMP-5. 05 NameLength      PIC 9(2)  COMP-5. *  Receives null-terminated name of match found. 05 FileName        PIC X(13). PROCEDURE DIVISION. DISPLAY &quot;The subdirectories are:&quot; *   Find the first normal or directory file. CALL &quot;__DosFindFirst&quot; USING BY VALUE    Reserved, BY REFERENCE Matches, BY VALUE    BufferLength, BY REFERENCE Buffer, BY VALUE    Attribute, BY REFERENCE Handle, BY REFERENCE Pathname. PERFORM UNTIL RETURN-CODE NOT = 0 *      If file was a directory, display the name. IF FileAttribute = 16 THEN DISPLAY FileName END-IF *      Initialize buffer for next filename. MOVE &quot;            &quot; TO FileName *      Find the next normal or directory file. CALL &quot;__DosFindNext&quot; USING BY REFERENCE Matches, BY VALUE    BufferLength, BY REFERENCE Buffer, BY VALUE    Handle END-PERFORM. STOP RUN. Copyright Microsoft Corporation 1990.