Wednesday, May 8, 2013

Batch Script to Check Empty File

This post deals with a simple batch script checking whether the incoming files are empty or not.This script performs the following basic things:

1. It would read the files sent as a prompt via command task in Informatica.
2. It would exit with an errorlevel(returncode) 0 if either of the file/or both files is/are not empty.This essentially means our command task would succeed and the main session would start running.
3. However if both files are empty, script would return an errorlevel of 1. This would fail the command task as we had checked the properties tab of the command task to 'Fail task if any command fails'.

I would post the original script below and then would explain it:

@echo off
set file1="%1"
set file2="%2"
set minbytesize=20

FOR /F "usebackq" %%A IN ('%file1%') DO set size1=%%~zA
FOR /F "usebackq" %%A IN ('%file2%') DO set size2=%%~zA

if %size1% LSS %minbytesize% (
if %size2% LSS %minbytesize% (
 echo.File size %size1% and ^< %minbytesize%
echo fail
exit 1
) ELSE (
echo.File size %size2% and ^>= %minbytesize%
echo pass
exit 0
)
)ELSE (
    echo.File size %size1% and ^>= %minbytesize%
echo pass
exit 0
)


  • The command @echo off turns off the prompt to display each command on screen. If its set to on we would see each command that gets executed, displayed on screen.
  • Now to the script. We have defined two variables file1 and file2. These two variables would hold the prompts sent from command task i.e. it would have the file path. In this case lets say variable file 1 would have value C:\Informatica\PowerCenter8.6.0\server\infa_shared\Srcfiles\a.csv and variable file 2 would have value C:\Informatica\PowerCenter8.6.0\server\infa_shared\Srcfiles\b.csv (please see my previous post in case you are confused)
  • One more variable with the name minbytesize is defined and its assigned a value of 20 bytes.
  • In the next two lines of code which looks a bit weird, whta happens is a for loop to read the contents of the file, the size of the file is determined and assigned into two variables size 1 and size 2. 
  • Next lines of code are self-explanatory. It goes like this:
                If size of file1 is less than 20 bytes(
                #check whether file2 is also less than 20 bytes
                If  size of file2 is less than 20 bytes(
                echo the size of file1 (or file 2 or both as per your need) and exit with errorlevel 1(failure;command task fails)
               )else
                #this means file 2 is greater than 20 bytes
                 (
                 echo the size of file 2 and exit with errorlevel 0(success; command task succeeds)
                  )else
                 #this means file 1 itself is greater than 20 bytes
                (
                  echo the size of file 1 and exit with errorlevel 0 (success; command task succeeds)
                )

Kindly provide your valuable suggestions as comments. Some wise guy has rightly said 'To err is Human'. So please feel free to correct me wherever you feel I have made a mistake or if there would have been an easier solution.








Sunday, May 5, 2013

Command task usage in Informatica which calls the empty file check batch script

Prior to my usage of command task, I was quite ignorant and skeptical about its usefulness. But once I had a chance to use this in my mapping and that’s when I realized how important command task could be :-)

Basically, the command task lets you specify one or more shell commands (for Informatica servers installed in UNIX machines) or one or more batch commands (for Informatica server installed in Windows machine) to run during the workflow.

For instance, you could just run a simple command for copying a file from one folder to another or delete a file; prior to a session run or after a session run. Given below is the scenario where we did successfully implement command task which subsequently calls a batch script to check whether the input files are empty. 

Objective: Informatica mapping had two flat file as sources. We had to check whether any of the flat files were empty. If and only if both files were empty, we had to fail the mapping. Else this command task must get executed and the mapping must run.

How we did it:
Step 1: We had two files as source downloaded from SAP server to Informatica's default source file directory '$PMSourceFileDir\'(How we did that will be discussed in another post)

Step 2: Prior to the run of original mappings session, we created a command task by the name ‘cmd_CHK_EMPTY’. Command task can be created in Workflow manager.
 Just go to
Tasks--> Create
A dialog box will appear as below:


Enter a name. Click on create and then done. Command task will be created.

Step 3: In the 'Commands' tab of the task, add a command and call the script to check the emptiness of file.In our case, we had to check the size for two source files. So these two file names along with their whole path were provided in the command.  

For eg: Let us say, we have saved our script that checks whether the file is empty  in the following folder:
C:\Informatica\PowerCenter8.6.0\server\infa_shared\Scripts
with the name 'SAPemptychk.bat' This would form our first argument. 
Our next two arguments would be the path of the source files. If the name of the files are 'a.csv' and 'b.csv' and if they are saved in the default folder 'Srcfiles', then this is how our command task's editor would finally look like: 



Step 4: Last but not the least, in the properties tab of the task, tick the option 'Fail task if any command fails' so that the Integration service would fail the command task if the script fails. This is necessary since if the script recognizes both files to be empty we need the mapping to fail.

In my next post, I would be delving into the details of the script 'SAPemptychk.bat'. Kindly provide your valuable suggestions as comments. Some wise guy has rightly said 'To err is Human'. So please feel free to correct me wherever you feel I have made a mistake or if there would have been an easier solution.