How to Get the Volume Identifier of a Tape

Article ID: 51237

Q: Is there a way to get the volume identifier of a tape with a CL program?

A: Yes. Usually when people ask this question, it's because they want to make sure that a certain tape is in the drive. There are two ways to approach that. The first way is to predetermine which volume name should be in the drive and then make sure it's there. The second way is to retrieve the volume name and then ask the user if it's the correct volume. Both of these are accomplished with the CHKTAP command in CL.

It's very common to check for a predetermined volume name. For example, if you run a nightly backup program, you may want to check for a particular tape on a particular night of the week. In a situation like that, you can use CHKTAP's VOL parameter to check for the right one. This is illustrated in the following code:

PGM

    DCL VAR(&VOLNAME) TYPE(*CHAR) LEN(6)
    DCL VAR(&DOW)     TYPE(*CHAR) LEN(4)

    /* MAKE A VOLUME NAME OF "MONBAK", "TUEBAK", ETC */

    RTVSYSVAL  SYSVAL(QDAYOFWEEK) RTNVAR(&DOW)
    CHGVAR VAR(&VOLNAME) VALUE(%SST(&DOW 2 3) *CAT 'BAK')

    /* CHECK TO SEE IF THAT IS IN THE TAPE DRIVE. IF NOT,  +
       ISSUE AN ERROR MESSAGE.                            */

    CHKTAP DEV(TAP01) VOL(&VOLNAME)
    MONMSG MSGID(CPF6720) EXEC(DO)
        SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) +
                  MSGDTA('WRONG TAPE!') MSGTYPE(*ESCAPE)
    ENDDO

    /* OTHERWISE, DO BACKUP HERE ...  */

    SAVLIB LIB(LIB1 LIB2) DEV(TAP01)

ENDPGM

The alternative, of course, is to retrieve the volume name of the tape. To do that, use the default value of *MOUNTED for the VOL parameter of the CHKTAP command. When the command completes successfully, it'll send a completion message back to the CL program that tells you which tape it found.

You can then use the RTVMSG command to retrieve the message data from the completion message into your program. The following sample program demonstrates this technique:

PGM
    DCL VAR(&VOLNAME) TYPE(*CHAR) LEN(6)
    DCL VAR(&MSGDTA)  TYPE(*CHAR) LEN(300)
    DCL VAR(&MSGID)   TYPE(*CHAR) LEN(7)
    DCL VAR(&REPLY)   TYPE(*CHAR) LEN(1)

    CHKTAP DEV(TAP01)

    RCVMSG MSGTYPE(*LAST)  +
           RMV(*NO)        +
           MSGDTA(&MSGDTA) +
           MSGID(&MSGID)

    IF (&MSGID *NE 'CPC6778') THEN(DO)
        SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) +
                  MSGDTA('Unable to Retrieve Volume ID!')
    ENDDO

    CHGVAR VAR(&VOLNAME) VALUE(%SST(&MSGDTA 11 6))

    SNDUSRMSG MSG('Volume in drive is' *BCAT &VOLNAME *BCAT +
                  'is that correct?')                       +
              VALUES(Y N)                                   +
              DFT(N)                                        +
              MSGTYPE(*INQ)                                 +
              MSGRPY(&REPLY)

    IF (&REPLY *EQ 'N') DO
       RETURN
    ENDDO

    SAVLIB LIB(LIB1 LIB2) DEV(TAP01)
ENDPGM

ProVIP Sponsors

ProVIP Sponsors