Solved

Editing a file using C# or VB

Posted on 2014-02-03
7
374 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

756 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