Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Editing a file using C# or VB

Posted on 2014-02-03
7
Medium Priority
?
383 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 45

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:Mihai Stancescu
Mihai Stancescu earned 375 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 375 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

885 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