Would like to not create an empty csv file

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

Moderators: chulett, rschirm, roy

PAULOM
Participant
Posts: 33
Joined: Thu Jul 11, 2013 2:03 am

Would like to not create an empty csv file

Post 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
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Unfortunately no, the empty text file will be created by DataStage.
PAULOM
Participant
Posts: 33
Joined: Thu Jul 11, 2013 2:03 am

Post by PAULOM »

It's not possible to create a job for checking if the file is empty or not?
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
PAULOM
Participant
Posts: 33
Joined: Thu Jul 11, 2013 2:03 am

Post by PAULOM »

Ok thanks, it's possible to know if the file is empty although the column name ?
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
PAULOM
Participant
Posts: 33
Joined: Thu Jul 11, 2013 2:03 am

Post by PAULOM »

Thanks chulett !
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Since MKS gets installed on a Windows server you can use "wc -l <filename>" platform independantly to get empty files.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

That may not be true if you have a 'Server only' license.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post 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.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
PAULOM
Participant
Posts: 33
Joined: Thu Jul 11, 2013 2:03 am

Post 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.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post 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
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post 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.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Well, heck, of course - the z modifier! Why didn't I think of that?

:P
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply