Page 1 of 1

Checking For Existing Rows In an Input File Before Executing

Posted: Thu May 31, 2007 10:45 am
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

Posted: Thu May 31, 2007 10:54 am
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.

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

Posted: Sat Jun 02, 2007 4:55 am
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--------------------------

Posted: Sat Jun 02, 2007 5:17 am
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)

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

Posted: Sun Jun 03, 2007 8:02 am
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)