Checking For Existing Rows In an Input File Before Executing

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
dtatem
Participant
Posts: 54
Joined: Wed Jun 09, 2004 7:50 am

Checking For Existing Rows In an Input File Before Executing

Post by dtatem »

Has anyone implemented this before:

Input file will be placed on the Unix server. On a daily basis, the file might be empty. If the file is empty and my sequencer runs, don't abort the job since there are no records in the file to be process but terminate normal, else process existing rows

I realized that this might have to be done during some kind of script...hence my limitation...I am not a shell script writer. Can someone provide some kind of feedback on how to implement this??

thanks,
dtatem
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

You can find different ways of checking the file size by doing a search. Keywords of search are "check for empty file". Depending upon the result, you can branch your execution.
PS: It does not have to be a script, even a BASIC routine will do the trick. Search for it.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
sachin1
Participant
Posts: 325
Joined: Wed May 30, 2007 7:42 am
Location: india

Re: Checking For Existing Rows In an Input File Before Execu

Post by sachin1 »

hello i am not aware how job sequencer or Routine_Activity works in datastage, but i think below code will enable you to check the file size, and then do further processing.


name of routine: file1
-----------------------code of routine file1---------------------------------
stat=''
if Arg1=@null or Arg1="" then
Ans=0;
PRINT "Enter proper file name"
goto file1:
end
Else
OPENSEQ Arg1 TO test THEN STATUS stat FROM test THEN PRINT ''

field6 = stat<6,1,1>
field8 = stat<8,1,1>
field5 = stat<5,1,1>
PRINT "filesize:": field6
PRINT "userid:": field8
PRINT "permissions:": field5

if trim(field6)=0 then

PRINT " FILE IS EMPTY"
end
CLOSESEQ test
Ans ='STATUS() IS ':STATUS()
end

file1:
----------------------------end of routine file1--------------------------
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Welcome aboard.

All DataStage processes are background processes, so INPUT and PRINT statements don't really do it. However, well spotted that field #6 of the dynamic array returned by the STATUS statement can return the size of the file.

The following function can be called from a Routine activity. Create a server routine whose type is "transform function".

Code: Select all

FUNCTION FileIsZeroSize(Pathname)
* Returns 1 if file exists and is of zero size, returns 0 if file exists and is larger.
* Returns @NULL if file can not be opened.

OpenSeq Pathname To filevar
On Error
   Ans = @NULL
End
Then
   Status FileData From filevar
   Then
      Ans = (FileData<6> = 0)
   End
   Else
      Ans = @NULL  ; * could not stat file
   End
   CloseSeq filevar
End
Else
   If FileInfo(filevar, 0) Then CloseSeq filevar
   Ans = @NULL
End  ; * end of OpenSeq statement

RETURN(Ans)
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
JoshGeorge
Participant
Posts: 612
Joined: Thu May 03, 2007 4:59 am
Location: Melbourne

Re: Checking For Existing Rows In an Input File Before Execu

Post by JoshGeorge »

Modify that this way and try. If Ans = 0, file is not empty, if Ans = 1 then empty else any error Ans = 9.
sachin1 wrote: -----------------------code of routine file1---------------------------------

Code: Select all


**Arg1 = PathPlusFileName 
Ans = 0
OpenSeq PathPlusFileName To FileVar Then 
         STATUS stat FROM FileVar THEN
           if stat<6,1,1> = 0 then 
           Ans = 1
           End Else
		Call DSLogInfo("File not empty","JobControl")
           End
         End        
  
Else     
    Ans = 9
    Call DSLogWarn("File Open error : ":PathPlusFileName ,"JobControl")
End
You can try any of the below unix commands also

ExCmnd = "wc -l ":pathplusfilename

** You can use -
** "test -z ":pathplusfilename or
** "[-z ":pathplusfilename" ]"

Call DSExecute ("UNIX",ExCmnd,Output,SystemReturnCode)
Joshy George
<a href="http://www.linkedin.com/in/joshygeorge1" ><img src="http://www.linkedin.com/img/webpromo/bt ... _80x15.gif" width="80" height="15" border="0"></a>
Post Reply