1,150 pts.
 checking for a folder in IFS
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
ASKED: April 20, 2010  8:56 AM
UPDATED: April 20, 2010  9:08 PM

Answer Wiki:
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
Last Wiki Answer Submitted:  April 20, 2010  6:34 pm  by  TomLiotta   108,370 pts.
All Answer Wiki Contributors:  TomLiotta   108,370 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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:

 7,185 pts.

 

that should be

OBJ(’ifs_path/ifs_object_name')
 7,185 pts.

 

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,540 pts.

 

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 pts.