I have a SQL Server Agent job that calls an SSIS package.The first task of the SSIS package is a C# script that checks for the existence of a file in a folder. The file may or may not exist in the folder.
If the file exists the task completes successfully and the next task in the package is invoked.
The job retries a number of times before terminating.
The issue I have is that if the file does not exist , when the job finishes it writes an entry in the History Log that the “Job Failed”.The job is scheduled to run every day, but the file will appear only once a month, and which day that happens is unknown.So I end up with a History Log with job fail entries every day except once a month.As far as I am concerned , the job is a success if it runs, if no file exists, that is not a failure in this context. So I do not want a job failure written to the History Log of the job.
I still want the job the package to terminate at the first task if a file is not found, but I do not want fail to be written to the History Log of the Job.
Is there some amendment to the C# script I can make to achieve this?
Or is there some other way?
I have been trying to implement the recommendations at these locations below but without success so far, they seem to point in the direction of creating
an event handler to achieve this..
http://sqlblog.com/blogs/rushabh_mehta/archive/2008/04/24/gracefully-handing-task-error-in-ssis-package.aspx
http://dougbert.com/blog/post/Faking-success-in-SSIS.aspx
Any guidance greatly appreciated.
Thank you
The C# script is below
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace ST_e72b194f12ee4fbab7090f4187b09a3c.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
private Int32 fileIndex;
public void Main()
{
String[] DEPOSFiles = Directory.GetFiles(Dts.Variables["SourceDataFileFolder"].Value.ToString(), "CMGDEPOS_*");
if (DEPOSFiles.Length != 0)
{
for (int i = 0; i < DEPOSFiles.Length; i++)
{
File.Move(DEPOSFiles[i],DEPOSFiles[i].Replace("DEPOS", "Corpos"));
}
}
String[] opicsFiles = Directory.GetFiles(Dts.Variables["SourceDataFileFolder"].Value.ToString(), Dts.Variables["LogicalFileName"].Value.ToString() + "_*");
FileInfo file;
if (opicsFiles.Length != 1)
{
try
{
Int32 fileLen = 100;
for (int i = 0; i < opicsFiles.Length; i++)
{
if (opicsFiles[i].Length < fileLen)
{
fileLen = opicsFiles[i].Length;
fileIndex = i;
}
}
if (Regex.Matches(opicsFiles[fileIndex], "_").Count == Regex.Matches(Dts.Variables["LogicalFileName"].Value.ToString(), "_").Count + 1)
{
file = new FileInfo(opicsFiles[fileIndex]);
}
else
{
throw new ArgumentException("No source data files exist that match the LogicalFileName(" + Dts.Variables["LogicalFileName"].Value.ToString() + ")", "LogicalFileName");
}
}
catch
{
throw new ArgumentException("No source data files exist that match the LogicalFileName(" + Dts.Variables["LogicalFileName"].Value.ToString() + ")", "LogicalFileName");
}
}
else
{
if (Regex.Matches(opicsFiles[0], "_").Count == Regex.Matches(Dts.Variables["LogicalFileName"].Value.ToString(), "_").Count + 1)
{
file = new FileInfo(opicsFiles[0]);
}
else
{
throw new ArgumentException("No source data files exist that match the LogicalFileName(" + Dts.Variables["LogicalFileName"].Value.ToString() + ")", "LogicalFileName");
}
}
while (IsFileLocked(file))
{
}
Dts.Variables["SourceDataFilePath"].Value = file.FullName;
Dts.TaskResult = (int)ScriptResults.Success;
}
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}
}
}
![]()