Order of files using READDIR

5 pts.
CL Program
I am receiving multiple files and placing them in a directory on the IFS. In a CL, I open the directory OPENDIR then READDIR to process all files. It begins reading the last file written when I need it to start with the first file written. How can this be solved in a CL program?

Answer Wiki

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

I had this same issue. First you need to find out when it was created. IFS objets are different that regular iseries objects when using DSPOBJ. You can not do this for IFS files. I have a program that will work. It writes the “file name” etc to a file. Then you can sort it however you want. (based on creation date) then read this file in a cl program. Email me if you want the program. Rkoontz@ceioh.com


Well heres the program. Just replace the DSPLY with your file…. You will need to add this to your RPG program where you read the IFS. I have one of those programs also if you want. Where i hard coded the IFS object is where you want to strat the logic. If you have any questions email me.

D UnixToRpg PR Z
D UnixTime 10U 0 value
D Epoch c z’1970-01-01-00.00.00′
DPause S 1
Dst ds likeds(statds)
Dts S Z
DUts S like(st_atime)
D stat PR 10I 0 ExtProc(‘stat’)
D path * value options(*string)
D buf * value
D p_statds S *
D statds DS BASED(p_statds)
D st_mode 10U 0
D st_ino 10U 0
D st_nlink 5U 0
D st_pad 2A
D st_uid 10U 0
D st_gid 10U 0
D st_size 10I 0
D st_atime 10I 0
D st_mtime 10I 0
D st_ctime 10I 0
D st_dev 10U 0
D st_blksize 10U 0
D st_allocsize 10U 0
D MyStat S like(statds)

if stat(‘/spool/Whateverfile.TXT’:
%addr(mystat)) < 0 ;
// Do error routine

p_statds = %addr(mystat);
dsply Pause ;
*INLR = *ON;
// ***********************************************************
// * Converts Date From UNIX Time Format To Something Readable
// ***********************************************************

// Last Access Date
ts = UnixToRpg(st_atime);
dsply (‘atime = ‘ + %char(ts:*ISO) );

// Memeber Date
ts = UnixToRpg(st_mtime);
dsply (‘mtime = ‘ + %char(ts:*ISO) );

// Created Date
ts = UnixToRpg(st_ctime);
dsply (‘ctime = ‘ + %char(ts:*ISO) );


P UnixToRpg B
D UnixToRpg PI Z
D UnixTime 10U 0 value

D Hours 10I 0
D Mins 10I 0
D Secs 8F
D fc 12A options(*omit)

D Epoch c z’1970-01-01-00.00.00’
D offset s 10I 0 static inz(-1)
D Hours s 10I 0 static
D Mins s 10I 0 static
D Secs s 8F static
D retval s Z
if (offset = -1);
CEEUTCO(Hours: Mins: Secs: *OMIT);
offset = secs;

retval = Epoch
+ %seconds(UnixTime)
+ %seconds(offset);
return retval;

From what I’ve been able to find, readdir() depends on the file system to determine the order, which isn’t necessarily predictable. If you have to have the file names in a specific order, I believe you’d need to build an intermediate list as you read them, and then sort that list so that you could process in the desired order.

See this for more info:


Discuss This Question:  

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.

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.

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


Share this item with your network: