5 Replies Latest reply on Aug 3, 2020 8:20 PM by Saurabh Shrivastava

    Check for the existence of a file

    Saurabh Shrivastava Active Member

      Hello All,

       

      I have a command task in my workflow. This command task would check for a file at a specific location. if a file exists then it would execute the relevant pipeline and if the file does not exists then another pipeline in workflow should be executed

       

      Please refer below details:

      Command Task Name: Check _For_File

      logic to check for file:

      if [ -f /data/infa_shared/SrcFiles/MDM_TO_DP/DASH_WEBFOCUS/TNC_DASH_Extract*.csv ];

      then

           exit 1

      else

           exit 0

      fi

       

      Note: There can be multiple files at location with name format TNC_DASH_Extract_, so i have used TNC_DASH_Extract*.csv in If condition

       

      Requirement: If a file or files exists then pipeline highlighted as 1 should be executed and if file/s does not exists then pipeline 2 should be executed as per below screenshot.

      The link task condition from command task Check_For_File to to the next session is as below

      for session s_m_TNC_DASH_EX_WebFocus: $Check_For_File.ErrorCode=0 AND $Check_For_File.Status=SUCCEEDED

       

      for session s_m_TNC_VEH_NON_TNC_CNTS_ORCL_RS1: $Check_For_File.ErrorCode=1 AND $Check_For_File.Status=SUCCEEDED

       

      The If else condition present in command task always returns 0 and so whether file is present at location or not, it always executes the pipeline 1.

       

      I think If Else condition is not working correctly or there is some issue in the condition of link task.

       

      Please help guyz or suggest some approach.

        • 1. Re: Check for the existence of a file
          Nico Heinze Guru

          The workflow links work exactly as they should. Your condition is "wrong" in the sense that what you want to achieve cannot be implemented exactly as you tried to do it:

          The exit code of a command in a command task leads to $... .STATUS = SUCCEEDED if the exit code is 0 and $... .STATUS = FAILED if the exit code does not equal 0.

          There is no provision of distinguishing between more than one exit code != 0, you can only distinguish between SUCCEEDED (exit code = 0) and FAILED (exit code != 0), nothing else.

           

          My suggestion is to go a different way.

          Implement the command task to check whether source files exist or not. Let this command task perform a "exit 0" if no source files exist.

          Now check in a workflow link condition whether STATUS = SUCCEEDED; if so, execute pipeline 2.

           

          If source files do exist, run almost the same command once more, but this time have the script perform a "exit 0" if the source files do exist. If the exit code of the script is 0, execute pipeline 1.

          If the exit code does not equal 0 (i.e. STATUS = FAILED), then there was some technical problem (maybe a permission issue with the source file directory, maybe a wrong source path... whatever), and you should execute a third pipeline where e.g. some error email is sent and the workflow finishes with status FAILED.

          This way you can cleanly distinguish between "no source files available", ">= 1 source file available", and "technical problem occurred".

           

          Regards,

          Nico

          1 of 1 people found this helpful
          • 2. Re: Check for the existence of a file
            Saurabh Shrivastava Active Member

            Hi Nico,

             

            There is no one who can write a script to perform "exit 0"...

            Any other way..?

            • 3. Re: Check for the existence of a file
              Santosh Gade Seasoned Veteran

              Is your command task actually failing ? Is the property "Fail task if any command fails"  enabled

              1 of 1 people found this helpful
              • 4. Re: Check for the existence of a file
                Nico Heinze Guru

                Saurabh,

                 

                what do you mean? Please explain with an example.

                 

                Thanks,

                Nico

                • 5. Re: Check for the existence of a file
                  Saurabh Shrivastava Active Member

                  The issue is resolved now. Below are the resolution steps that I have taken:

                   

                  1. Property "Fail task if any command fails" was not enabled in command task named Check_For_File. Enabled it now.

                  2. Updated the condition to check the existence of multiple files in command task as below

                  if ls $$src_file_dir/fileName*.csv  1> /dev/null 2>&1;
                  then
                       exit 0
                  else
                       exit 1
                  fi

                  3. Updated the condition on Link tasks(from command task to respective pipelines) as below

                  For Success Pipeline(Pipeline#1): $Check_For_File.ErrorCode=0

                  For Failure Pipeline(Pipeline#2): $Check_For_File.ErrorCode!=0

                   

                  Thank you all for suggestions.

                   

                  Cheers