Solved

Editing a file using C# or VB

Posted on 2014-02-03
7
368 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

11 Experts available now in Live!

Get 1:1 Help Now