Scenario:
We have received text or csv file. The file has some company information,date created etc. before the data rows start. Our goal is to delete these information rows and regenerate file start from header row.
Fig: Source File With Company Information
Solution:
We will be using Script Task to Delete the Top N rows from text file. We will be reading all the data from the file in string type array and then overwrite the original file after removing Top N rows.
Step 1:
Create two variables as shown in figure.VarFilePath is variable holding the file path. VarDeleteTopNRows is variable that will hold the number of rows you want to delete from starting of file.
Fig 1: Create variables in SSIS Package To Delete Top N Rows from Flat File by using Script Task
Step 2:
Bring the Script Task to Control Flow Pane. Map the above Variables as shown to ReadOnlyVariables in Script Task.
Fig 2: Map Variables in Script Task
Paste the below Code. I have bold the code that I wrote, rest of the code is auto generated by Script Task.
#region Namespaces using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using System.IO; using System.Linq; #endregion namespace ST_c62f3dcfb0964917aade179aac4edfab { [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { public void Main() { // TODO: Add your code here string FilePath = Dts.Variables["User::VarFilePath"].Value.ToString(); Int32 DeleteTopNRows = Convert.ToInt32(Dts.Variables["VarDeleteTopNRows"].Value); string[] lines = System.IO.File.ReadAllLines(FilePath); lines = lines.Skip(DeleteTopNRows).ToArray(); System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath); foreach (string line in lines) { // MessageBox.Show(line.ToString()); file.WriteLine(line); } file.Close(); Dts.TaskResult = (int)ScriptResults.Success; } enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; } }
Save the code. Run the script task and once completed. Check the file again.
Fig 3: Top 3 Rows Deleted By SSIS Package.
You can change the value of variable VarDeleteTopNRows according to your requirements.
Saved as a favorite, I really like your site! Clicking Here: Top Five Funny Encryption Software Quotes & Here Are Five Ways To How To Encrypt A Password For Free Faster.
ReplyDeletebrother iam getting an exception,what should i do
ReplyDeletebest electric blankets
ReplyDeleteYour work is very good and I appreciate you and hopping for some more informative posts... Thank you ...
ReplyDeleteDigital Marketing Course in Pitampura | Digital Marketing Training in Pitampura | Digital Marketing Institute in Pitampura
Hello,
ReplyDeleteI am very thankful for the effort put on by you, to help us, Thank you so much for the post it is very helpful, keep posting such type of articles.
Mehndi Artist in Delhi | Best Mehndi Artist in Delhi | Simple Mehndi Design | Mehndi Service at Home in Delhi
I am really thankful to the holder of this web site who has shared this enormous article at here. Maths Tuition in Rohini | Best Maths Tuition in Rohini | Maths Tuition Center in Rohini | Best Maths Tuition Center in Rohini | Best Maths Tutor in Rohini
ReplyDeleteit is very interesting to read your article about yoga. it is very informative article. thank ypu so much for sharing Maths Coaching in Rohini | Best Maths Coaching in Rohini | Maths Coaching Center in Rohini | Maths Coaching Classes in Rohini | Best Maths Coaching Classes in Rohini | Maths Tuition in Rohini | Best Maths Tuition in Rohini | Maths Tuition Center in Rohini | Best Maths Tuition Center in Rohini | Best Maths Tutor in Rohini
ReplyDeleteExcellent post. This article is very helpful for people and also for me. I get some useful knowledge in this post. Thanks for sharing the informative topic, by the way... Digital Marketing Course in Delhi | Web Designing Course in Delhi | Graphic Designing Course in Delhi | English Speaking Course in Delhi | Data Science Course in Delhi | Machine Learning Course in Delhi | Python Course in Delhi
ReplyDeleteVery informative post I was searching for a few days. I am sure it will help me.
ReplyDeleteMaths Tuition in Rohini | Biology Tuition Near Me | Physics Classes in Rohini | BBA Coaching Near Me | Chemistry Coaching in Rohini | Economics Tuition Near Me | Accounts Tuition in Rohini
Very informative post I was searching for a few days. I am sure it will help me.
ReplyDeleteMaths Tuition in Rohini | Biology Tuition Near Me | Physics Classes in Rohini | BBA Coaching Near Me | Chemistry Coaching in Rohini | Economics Tuition Near Me | Accounts Tuition in Rohini
This post is very useful and informative. what is fantastic post, this is so chock full of useful information I cannot wait to dig deep and start utilizing the resource give me.your exuberance is refreshing.
ReplyDeleteDigital Marketing Course in Pitampura | Digital Marketing Institute in Pitampura | Digital Marketing Training in Pitampura
Hello,
ReplyDeleteThese trends are so useful and beneficial for everyone, Thank you so much for this guide ...
Have a Good Day...
Digital Marketing Course in Janakpuri | | Digital Marketing Course in GTB Nagar
Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
ReplyDeletesql server dba online training
SQL dba online course
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
ReplyDeleteDigital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
Digital marketing agecny
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeleteBest quoting software
It works! Thanks!
ReplyDeleteHello,
ReplyDeleteThanks a lot dude, It means a lot of me, Its amazing, I have learned many things on your blog. If you don't mind kindly visit my website...
Graphic Design Course in Delhi
Diploma Course in Graphic Design
Certificate Course in Graphic Design
Adobe Illustrator Course in Delhi
Adobe Photoshop Course in Delhi
InDesign Course in Delhi
CorelDraw Course in Delhi
HTML Course in Delhi
Bootstrap Course
It leaves me with some encoding error characters in my file after running the script task. not there before but there after?
ReplyDeleteSo there is no place for a text qualifier so when converting an excel file to .csv, in my case, some rows had columns shifted and gave an unexpected result. do you know how to fix this?
ReplyDeleteSo there is no place for a text qualifier so when converting an excel file to .csv, in my case, some rows had columns shifted and gave an unexpected result. do you know how to fix this?
ReplyDeletegood Company Registration in Bangladesh
ReplyDeleteWhatsapp Number Call us Now! 01537587949
Graphics design course
Great, thanks for sharing this post.Much thanks again. Awesome.
ReplyDeleteonline training in java
online training on java
Your work is very good and I appreciate you and hopping for some more informative posts. If you want to crack NEET Medical Exam in first attempt then Join YVS INSTITUTE Best NEET Coaching in Delhi
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteViolin Shop Honolulu
ReplyDeleteAre you finding online music classes for violin? Violin shop Honolulu offers online classes and face-to-face classes at most at an affordable price. We are giving online classes on face time, Skype, zoom, and Hangout. Get in touch with us at www.honolulumusiclessons.com.
Ziyyara Edutech is a worldwide online home tuition provider. Our main focus is not only to help students to study, but also to support them to get maximum marks and prepare them in a manner that they can clear their exams with flying colors. Call us @ +91 9654271931
ReplyDeleteThanks for the information. I really like the way you express complex topics in lucid way. It really helps me understand it much better way. kailua-kona homes for sale
ReplyDeleteit is very informative blog.thanks for sharing. Python course in janakpuri
ReplyDelete