The need of objects that exist on the IFS increases daily, and along with those needs comes uncertain programmer...
headaches. Using CHKOBJ to determine the existence of an object is limited to HFS objects, and the ability to use that command on the IFS is non-existant. I happened to have read a similar tip where another programmer used an API to accomplish the task of determining whether an object existed on the IFS.
The excerpt below is a nice little program, which is limited to a 30 alpha string in this example, that uses the CHKIN and CHKOUT commands to determine the existence of an object. If the CHKOUT command is successful, then the object must be checked back in via the CHKIN to prevent object locks.
PGM PARM(&STMF &CODE) DCL VAR(&STMF) TYPE(*CHAR) LEN(30) DCL VAR(&CODE) TYPE(*CHAR) LEN(1) CHKOUT OBJ(&STMF) MONMSG MSGID(CPFA0A9) EXEC(CHGVAR VAR(&CODE) + VALUE('0')) /* NOT FOUND */ MONMSG MSGID(CPF0000) EXEC(DO) /* ANY OTHER ERROR + (IN USE) */ CHGVAR VAR(&CODE) VALUE('1') /* IT ALREADY EXISTS */ CHKIN OBJ(&STMF) /* RELEASE USE */ MONMSG MSGID(CPF0000) ENDDO ENDPGM