Page 1 of 4

Would like to not create an empty csv file

Posted: Fri Jul 19, 2013 2:39 am
by PAULOM
Hi,

I created a file csv but I would not like to create the latter when it's empty (only column name present), is it possible?

Thanks

Posted: Fri Jul 19, 2013 4:56 am
by ArndW
Unfortunately no, the empty text file will be created by DataStage.

Posted: Fri Jul 19, 2013 6:27 am
by PAULOM
It's not possible to create a job for checking if the file is empty or not?

Posted: Fri Jul 19, 2013 6:33 am
by chulett
Of course it is, but that's not what you asked. An output file will always be created by a job. What you do afterwards is up to you - check for empty and delete it if you like after job.

Posted: Fri Jul 19, 2013 6:59 am
by PAULOM
Ok thanks, it's possible to know if the file is empty although the column name ?

Posted: Fri Jul 19, 2013 7:19 am
by chulett
Count the records. Typically 0 would mean empty but if you know there is a header record then 1 would equal empty. Easier on UNIX than Windows, however.

Posted: Fri Jul 19, 2013 7:51 am
by PAULOM
Thanks chulett !

Posted: Fri Jul 19, 2013 8:47 am
by ArndW
Since MKS gets installed on a Windows server you can use "wc -l <filename>" platform independantly to get empty files.

Posted: Fri Jul 19, 2013 9:22 am
by chulett
That may not be true if you have a 'Server only' license.

Posted: Fri Jul 19, 2013 4:05 pm
by ray.wurlod
Use an after-job subroutine. When a file is opened with OpenSeq statement the System() function can return the file size (among other things). Then use CloseSeq and execute a command to delete the file. Pass the file's pathname as the input argument.

Posted: Thu Aug 01, 2013 9:18 am
by PAULOM
I tried to make what you say, but It don't work !

Can you describe me, what i put in the after-job subroutine?
I can't find the DOS command for the size of files...

Thanks.

Posted: Thu Aug 01, 2013 9:26 am
by chulett
Why don't you show us what you tried, post the code? The DOS command DIR will show file sizes but you'd need some batch skills to create a useful check which is why Ray suggested the DataStage BASIC System() function.

Posted: Thu Aug 01, 2013 6:21 pm
by ray.wurlod
Actually it's the STATUS statement, rather than the System() function, that you need here. This statement returns a dynamic array of information about the file whose file handle is provided.

The following example code has minimum error handling, for purposes of clarity.

Code: Select all

SUBROUTINE DeleteIfEmpty(aPathname, aErrorCode)

* Setting error code to a non-zero value will abort the job.
aErrorCode = 0

OpenSeq aPathname To hFile
On Error
   aErrorCode = Status()
End
Locked
   aErrorCode = Status()
End
Then
   Status FileInformation From hFile
   Then
      FileSize = FileInformation<6>
      If FileSize = 0
      Then
         Shell = (If System(91) Then "DOS" Else "UNIX")
         Command = (If System(91) Then "DEL " Else "rm ") : aPathname
         CloseSeq hFile
         Call DSexecute(Shell, Command, Output, ExitStatus)
      End
   End
End
Else
   If Status() = 0 Then CloseSeq hFile
   aErrorCode = 2  ; * pathname does not exist or is not a file
End

RETURN

Posted: Thu Aug 01, 2013 7:31 pm
by ray.wurlod
You can, of course, test for the file size in a DOS batch file which you might invoke via the ExecDOS after-job subroutine. You can find the technique more fully documented in DOS help or via an internet search.

In essence the technique involves simply using the z modifier for a FOR variable reference. For example:

Code: Select all

set "filemask=myfile*.txt"
for %%A in (%filemask%) do if %%~zA==0 echo."%%A" is empty
The BASIC code I posted earlier has the advantage that it works on all platforms.

Posted: Thu Aug 01, 2013 7:41 pm
by chulett
Well, heck, of course - the z modifier! Why didn't I think of that?

:P