We have an SSIS job that takes data from a database and outputs it to a csv file and saves it to a file share. The last line in the csv we produce is a blank row and the consumers of this file want it removed. I need to figure out how, after we write the file to the directory, to modify the file and remove that last blank row. I believe you can do this in a script task but everything I have tried has failed, both VB and C#. Has anyone figured out how to do something like this?
I hope this works as I am shooting from the hip:
This should remove the last carriage return and line feed.
C#
string s = System.IO.File.ReadAllText([filePath]);
s=s.Substring(1,s.Length-2); //CRLF
System.IO.File.WriteAllText([filePath],s);
You can use a Conditional Split Transformation in between source and destination on a Data Flow pipeline to remove empty rows.
In the Split Transformation Editor specify a boolean expression with a Condition like
LEN([Col 1]) > 0 && LEN([Col 2]) > 0 ...
Bonus: If there is also bad data in the output file you may want to filter on a different length (as shown in the screenshot): Ref .
If this still does not work I suggest to try it with a C# script task along that lines:
try
{
string input = System.IO.File.ReadAllText("path");
string output = System.Text.RegularExpressions.Regex.Replace(input, @"^\s+$[\r\n]*", "", System.Text.RegularExpressions.RegexOptions.Multiline);
System.IO.File.WriteAllText("path", output);
}
catch (Exception e)
{
//Log..
Dts.TaskResult = (int)ScriptResults.Failure;
}
If you also want to remove the very last line-break use this pattern with the code above instead: @"^\\s+$[\\r\\n]*|[\\r\\n]$"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.