Microsoft KB Archive/63800

How to Rename/Copy Files & Directories in Protected Mode COBOL

PSS ID Number: Q63800 Article last modified on 07-13-1990

3.00 3.00a OS/2

Summary: A Microsoft COBOL version 3.00 or 3.00a protected mode program can rename a file or directory and copy files from one directory to another by calling the OS/2 API function DosMove. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2.

More Information: COBOL versions 3.00 and 3.00a can directly call OS/2 API functions by prefixing the name of the function with a double underscore (&quot;__&quot;) and linking with the PCOBOL.LIB and DOSCALLS.LIB libraries. The API function DosMove requires the following parameters:

The first two parameters are PIC X fields of variable length that must contain a null (X“00”). The function will include all the characters up to the null as the pathname. Since pointers (PTR) to these parameters are required, they must be passed using the BY REFERENCE clause. The second parameter is a PIC 9(8) COMP-5 field (double-word) and must be set to 0. Since a pointer to the parameter is not required, it must be passed using the BY VALUE clause. Note also that the parameters must be passed in the reverse order because OS/2 API functions use a calling convention that is the reverse of COBOL’s. NOTE: If a drive is included in the new pathname, it must match the drive specified in the old pathname or the current drive. For more information on calling OS/2 API functions from COBOL, see the file OS2API.DOC included with COBOL versions 3.00 and 3.00a. For more information on the API function DosMove, see Page 536 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The sample program below (RENAMDIR.CBL) lists all the subdirectories in the current directory. It then prompts you for one of those directories and what the new name is to be (8-character limit). Finally, the directory name is changed and a listing of all the subdirectories is displayed again, reflecting the name change. To compile and link the program, enter the following lines at the OS/2 command prompt: cobol renamdir; link /nop renamdir,,,pcobol doscalls;

Code Example
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. RenamDir. DATA DIVISION. WORKING-STORAGE SECTION. *  DosMove parameters. *  Null-terminated current directory name. 01 OldDirName. 05 OldName     PIC X(8). 05 ASCII0      PIC X             VALUE X&quot;00&quot;. *  Null-terminated new directory name. 01 NewDirName. 05 NewName     PIC X(8). 05 ASCII0      PIC X.  *   Reserved by OS/2, set to 0. 01 Reserved       PIC 9(8)   COMP-5 VALUE 0. *  X&quot;91&quot; parameters. *  Receives result of X&quot;91&quot; call. 01 Result         PIC 99     COMP-X. *  Function 35 is EXEC call. 01 Function       PIC 99     COMP-X VALUE 35. *  Parameter for function 35 of X&quot;91&quot; call. 01 Parameter91. *  Length of name of file to execute. 05 NameLength  PIC 99     COMP-X VALUE 6. *  Name of file to execute. 05 FileName    PIC X(6)          VALUE &quot;dir *.&quot;. *  Used to access bytes of OldDirName and NewDirName. 01 Byte           PIC 99     COMP-X. PROCEDURE DIVISION. *   Insert X&quot;00&quot; (null) in all bytes of OldDirName and *   NewDirName so the names passed to the API function will be  *    null-terminated even if directory name < 8 bytes long. PERFORM VARYING Byte FROM 1 BY 1 UNTIL Byte > 9 MOVE X&quot;00&quot; TO OldDirName(Byte:1) MOVE X&quot;00&quot; TO NewDirName(Byte:1) END-PERFORM. *   Clear screen and list subdirectories in current directory. CALL X&quot;E4&quot;. CALL X&quot;91&quot; USING Result, Function, Parameter91. *   Prompt for directory name to change and what to change it  *    to. DISPLAY &quot;Enter old directory name:&quot;. ACCEPT OldName. DISPLAY &quot;Enter new directory name:&quot;. ACCEPT NewName. *   Change name of directory and display listing of all *   subdirectories in the current directory. The new name *   of the changed directory will be displayed. CALL &quot;__DosMove&quot; USING BY VALUE    Reserved, BY REFERENCE NewDirName, BY REFERENCE OldDirName. MOVE 35      TO Function. MOVE 6       TO NameLength. MOVE &quot;dir *.&quot; TO FileName. CALL X&quot;91&quot; USING Result, Function, Parameter91. STOP RUN. Copyright Microsoft Corporation 1990.