Solved

Editing a file using C# or VB

Posted on 2014-02-03
7
372 Views
Last Modified: 2014-02-11
Dear Microsoft Visual C# 2008 and Microsoft Visual Basic 2008Experts;

I need to edit a .CSV file using the Script Task within SSIS 2008.  The file will be comma delimited (,) or carrot delimited (^), text qualifier is a quote (“), header row delimiter is a line feed (LF) and column names in the first data row.  Searching though the file there is a common pattern of strings that needs replacing; examples inside the square brackets would be [“”)] replaced with [)], [“”.] replaced with [.], [ “,””] replaced with [“,”] and [\r\n] replaced with a blank space.

I have seen examples of .vbs scripts using replacement like;
 if !foo!==ex3 set foo=ex5
or
 If InStr(strLine,""")")> 0 Then
    	strLine = Replace(strLine,""")",")")

I need something to go within the Script Task in SSIS 2008.
0
Comment
Question by:wdbates
7 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39832286
I would assume strLine contains characters that need replacing and I would nest the replace-function:
strLine = Replace(Replace(Replace(Replace(strLine, "old1", "new1"), "old2", "new2"), "old3", "new3"), "old4", "new4")

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39832416
ha ha, ;-)     caret not carrot
0
 

Author Comment

by:wdbates
ID: 39834557
Andy; you are correct!  And John, I need this in C# 2008 or VB 2008 and within SSIS script.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 11

Assisted Solution

by:Mihai Stancescu
Mihai Stancescu earned 125 total points
ID: 39834913
Hi,

You can also use regular expressions to replace text.
Using this patter will find all the required matches you need then you need to just replace them with what you need.
 var replacedString = "";
replacedString  = Regex.Replace(fileContents, @"\[“”\)\]", "[)]");
replacedString  = Regex.Replace(fileContents, @"\[“”\.\]", "[.]"); 
replacedString  = Regex.Replace(fileContents, @"\[ “,””\]", "[“,”]"); 
replacedString  = Regex.Replace(fileContents, @"[\r\n]", " ");

Open in new window


Hope this helps,
Mishu
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 125 total points
ID: 39835431
VB.NET (replace what's in the double quotes with whatever you need, add as many Replace calls as you need:
strLine = strLine.Replace("old1", "new1").Replace("old2", "new2").Replace("old3", "new3") ... .Replace("oldN", "newN")

Open in new window

0
 

Author Closing Comment

by:wdbates
ID: 39851103
Thanks guys.  Sorry for the delay.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question