Solved

Editing a file using C# or VB

Posted on 2014-02-03
7
369 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

943 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

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now