checking for a folder in IFS

1150 pts.
Tags:
IBM i
IFS Directory
IFS file list
V6R1
Hi, plz tell me how to check for the existence of a directory/folder in the IFS file system from a CL program.

Software/Hardware used:
IBM i,V6R1

Answer Wiki

Thanks. We'll let you know when a new response is added.

The CHKIN command can be helpful, but it also “checks in” the object. You can use CHKOUT followed by CHKIN when it succeeds in checking the object out.

For a long term viable solution, use the access() API. Here’s a trivial example:<pre>
pgm

dcl &fname *char 512
dcl &amode *int 4

dcl &F_OK *int 4 value( 0 )
dcl &W_OK *int 4 value( 2 )
dcl &x00 *char 1 value( x’00′ )

dcl &rc *int 4

/* chgvar &amode &F_OK */
chgvar &amode &W_OK
chgvar &fname ( +
‘/home/MyHome/p.txt’ *cat +
&x00 +
)

callprc ‘access’ ( +
&fname +
( &amode *byval ) +
) +
rtnval( &rc )

/* Note: &rc *eq x’00000000′=0 on success; x’FFFFFFFF’=-1 on fail… */

dmpclpgm

return

endpgm</pre>
This checks for ‘W’rite access capability to a streamfile named /home/MyHome/p.txt — the &F_OK mode value would check for simple accessibility/existence. Tests for existence assume that authority allows for at least *USE, otherwise you’re not going to see the object.

Note that the file name must be null-terminated. And note that the mode is supplied “by value” rather than by reference.

<a href=”http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/apis/access.htm”>Details of access()</a> are in with the other Unix-like APIs.

The procedure must be ILE CL (PDM member type CLLE). It could be compiled as a procedure that accepts a file name name as a parm. Or you might compile it as a *PGM and use it as the CPP for a command that returns a parm or sends *DIAG or *ESCAPE message.

Tom

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • BigKat
    I use the following for file, you may be able to tweak it for folders
    CHKIN      OBJ('ifs_path/ifs_object_name) 
    MONMSG     MSGID(CPFA0A9) EXEC(DO)               
      RCVMSG     MSGTYPE(*EXCP)                      
      GOTO       CMDLBL(EXIST)                       
    ENDDO      /* CPFA0A9 */                         
    GOTO       CMDLBL(NOTEXIST)
    
    EXIST:
    
    
    
    NOTEXIST:
    
    
    8,210 pointsBadges:
    report
  • BigKat
    that should be
    OBJ(’ifs_path/ifs_object_name')
    8,210 pointsBadges:
    report
  • DanTheDane
    Often this requirement is followed by the need to create the folder searched for. From my archives I found this cod
    PGM                                                     
    DCL        VAR(&ARKIV) TYPE(*CHAR) LEN(132)             
    DCL        VAR(&DATE) TYPE(*CHAR) LEN(6)                
    DCL        VAR(&YEAR) TYPE(*CHAR) LEN(4)                
                                                            
    RTVJOBA    DATE(&DATE)                                  
    CHGVAR     VAR(&YEAR) VALUE('20' *TCAT %SST(&DATE 1 2)) 
    CHGVAR     VAR(&ARKIV) VALUE('/WORK/ARKIV_' *TCAT &YEAR)
    CHKIN      OBJ(&ARKIV)                                  
    MONMSG     MSGID(CPFA0A9) EXEC(MD DIR(&ARKIV))          
    MONMSG CPFA0DA                                          
                                                            
    ENDPGM                                                  
    
    I believe the code is fairly straight forward to understand. DanF
    2,555 pointsBadges:
    report
  • LoraEstradaNapier

    I know this is two years after the original post, but for anyone who is looking for this answer and wants to know how to check for a FOLDER or DIRECTORY and not a document, this simple logic works.  I won't say I'm proud of it..... It is more "sleight of hand than anything".

    Pass this program a path name, such as '/System i Users/LNapier'.  The ChkOut command expects the OBJ parm to be a document, not just a directory.  So, if the path is a directory, ChkOut will issue CPFA0DA.

    Pgm (&In$Path &Out$IsDir)                                   
                 DCL        VAR(&In$Path) TYPE(*CHAR) LEN(512)  
                 DCL        VAR(&Out$IsDir) TYPE(*LGL)          
                                                                
         ChgVar &Out$IsDir '0'                                  
         CHKOUT     OBJ(&IN$PATH)                               
         MONMSG     MSGID(CPFA0DA) EXEC(ChgVar &Out$IsDir '1')  
         MONMSG     MSGID(CPFA09C CPFA09D CPFA09E CPFA0A1 +     
                      CPFA0A3 CPFA0A7 CPFA0A9 CPFA0AA CPFA0AB + 
                      CPFA0AD CPFA0B2 CPFA0BF CPFA1C5)          
                                                                
    EndPgm                                                      

     

    20 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following