Asked by:
SSIS Script Task - Not Getting Executed

Question
-
Hello All,
I have a script task in a for each loop container that calculates the filesize, and if the filesize is > 0 the subsequent tasks gets executed.
Here the code inside the script task is not getting executed and hence the file size is empty/0 and the control is not going to the subsequent tasks.
is this something to do the framework ? can anyone please help my solve this issue.
Thanks,
Rahul.
All replies
-
-
Hello,
thanks for the response.below is the code and screenshot.
public void Main()
{
try
{
string segment_separator = "";
string field_separator = "";
string component_separator = "";
string ValidEDILayout = "0";
string EDIHeaderValue = "";
var receivedFile = (string)Dts.Variables["User::strFileNameFull"].Value;
var fileInfo = new System.IO.FileInfo(receivedFile);
if (fileInfo.Length > 0)
{
string raw = File.ReadAllText(receivedFile);
EDIHeaderValue = raw.Substring(0, 3);
ValidEDILayout = "1";
if (EDIHeaderValue != "ISA")
{
ValidEDILayout = "0";
}
if (EDIHeaderValue == "ISA")
{
segment_separator = raw.Substring(105, 1);
field_separator = raw.Substring(3, 1);
component_separator = raw.Substring(104, 1);
}
}
Dts.Variables["User::intFileSize"].Value = fileInfo.Length;
Dts.Variables["User::strFieldDelimiter"].Value = field_separator;
Dts.Variables["User::strSegmentDelimiter"].Value = segment_separator;
Dts.Variables["User::strComponentDelimiter"].Value = component_separator;
Dts.Variables["User::blnValidEDILayout"].Value = ValidEDILayout;
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
Console.WriteLine($"The file could not be opened: '{e}'");
}
}
-
-
Hi Rahul,
Check it out the following c# code.
It checks for file existence, file attributes, error handling, logging, etc.
bool rc = true; try { var receivedFile = (string)Dts.Variables["User::strFileNameFull"].Value.ToString(); var fileInfo = new System.IO.FileInfo(receivedFile); if (fileInfo.Exists) { // Get file creation date Dts.Variables["User::FileCreationDate"].Value = fileInfo.CreationTime; // Get last modified date Dts.Variables["User::FileLastModifiedDate"].Value = fileInfo.LastWriteTime; // Get size of the file in bytes Dts.Variables["User::FileSize"].Value = fileInfo.Length; } else { rc = false; // Don't forget to tick/check off System::TaskName variable as ReadOnly in the Task Parameters Dts.Events.FireWarning(1, Dts.Variables["System::TaskName"].Value.ToString() , string.Format("File '{0}' does not exist", fileInfo.FullName) , "", 0); } } catch (Exception ex) { rc = false; // Don't forget to tick/check off System::TaskName variable as ReadOnly in the Task Parameters Dts.Events.FireError(18, Dts.Variables["System::TaskName"].Value.ToString() , ex.Message.ToString() , "", 0); } if (rc) { Dts.TaskResult = (int)ScriptResults.Success; } else { Dts.TaskResult = (int)ScriptResults.Failure; }
- Edited by Yitzhak Khabinsky Thursday, October 10, 2019 5:58 PM
-
-
-
-
-
Hi Rahul,
May I know how do you edit your Foreach Loop Container?
Please check if you can find files in the specified folder.
Best Regards,
Mona
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com- Proposed as answer by Mona LvMicrosoft contingent staff Tuesday, October 15, 2019 1:42 AM