2

First off, I am not a c# person, so please bear with me on this one. I need to replace the occurrences of "D:" with "d$" on a script task within SSIS. I sure use the replace function to do this, but the problem is, that this is having unintended consequences on another line.

For example, the script task sends out an email and the header of the email reads as \servername\d$ \further_path. The body of the email reads "UID: 1 : MESSAGE"

The line of code that sends the email reads like:

myHtmlMessage = new MailMessage(Dts.Variables["MailFromAddress"].Value.ToString(), Dts.Variables["MailRecipients"].Value.ToString(), Dts.Variables["MailSubjectSuccess"].Value.ToString(), Dts.Variables["MailBodySuccess"].Value.ToString().Replace("D:", @"\d$   "));

The current output that I get is:

Server Start Time: 3/21/2017 7:25:33 AM
Server End Time: 3/21/2017 7:27:39 AM
Total Run Time: 00:02:06.9402516
Log Folder: \\ServerNamed$\Apps\SSIS\Logs\

UId$ 2 - 

The intended output is:

Server Start Time: 3/21/2017 7:25:33 AM
Server End Time: 3/21/2017 7:27:39 AM
Total Run Time: 00:02:06.9402516
Log Folder: \\ServerNamed$\Apps\SSIS\Logs\

UID: 2 - 

Look at the log folder line and the UID line

When I use the replace function, the body line gets affected as well with the d$ symbol and that is what I am trying to avoid. Can I write a conditional REPLACE function in C# or, is there any other way to deal with this?

Thanks, RV.

rvphx
  • 2,324
  • 6
  • 40
  • 69
  • Can you please add your code – Samvel Petrosov May 30 '17 at 16:59
  • aded to question – rvphx May 30 '17 at 17:02
  • Can you please also add example with string for input and output(expected and received)․ Your question Is not completely understandable – Samvel Petrosov May 30 '17 at 17:05
  • Do you have an example input body? The example I provided doesn't handle D: within a word (e.g. "\\ServerNameD:\foo" won't be recognized by the regex parser). – Paurian May 30 '17 at 17:18
  • Technically, there is no input body as such. All the variable values are collected on the fly from the package. But the need is to replace only the D: with d$ on the Log folder line and no where else within the entire body. – rvphx May 30 '17 at 17:21
  • If the text has already been consolidated into a string and "Log Folder" is a line within it, you might have better results using "(^Log Folder.+)D:" for the pattern and "$1d$" for the replacement. Note that this will only work if you expect "D:" to appear but once. The "$1" is specifically to recall the text that was in the parenthesis, followed by the "d$" you requested to replace "D:". – Paurian May 30 '17 at 17:38
  • So you are getting `ServerNameD:\Apps...`? But what is your desired output, `ServerNamed$\Apps...`or `ServerName\d$\Apps...`? Your output says something but your code says otherwise. – Andrew May 30 '17 at 17:53

2 Answers2

2

Have you looked into regular expressions?

https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx

Here's an example of how you would use it... note that I haven't tested this to determine if it fully functions, but it should help you get started down this path:

E.G.

// Assign your strings into variables to make your code cleaner:
string fromAddress = Dts.Variables["MailFromAddress"].Value.ToString();
string recipients = Dts.Variables["MailRecipients"].Value.ToString();
string subject = Dts.Variables["MailSubjectSuccess"].Value.ToString();
string body = Dts.Variables["MailBodySuccess"].Value.ToString();

// Replace D: in body
string pattern = "(Log Folder.+)D:"; // Capture the D: only if it's on the Log Folder line.
string replacement = "$1\\d$   "; // What we're replacing with.
Regex rgx = new Regex(pattern);
body = rgx.Replace(body, replacement);

// Build my HTML message.
myHtmlMessage = new MailMessage(fromAddress, recipients, subject, body);

I hope this helps...

Note - you can learn much more about regular expression syntax at http://www.regular-expressions.info/ . It's worth looking into if you want to learn how these engines work, how they might differ, and the best syntax to use for finding specific expressions under certain context.

Paurian
  • 1,372
  • 10
  • 18
  • No - I didn't test any of this code - it's intended to direct the OP, not to do the work for him. – Paurian May 30 '17 at 17:14
  • Your code is not working. If you are providing example then add explanation how to do things instead of giving code saying here is e.g. and it is not working – Samvel Petrosov May 30 '17 at 17:16
  • Thanks, @S.Petrosov - great suggestion. I've edited (and fixed some code) so that it would be easier to understand its context. – Paurian May 30 '17 at 17:22
  • Let me try the suggestion real quick and get back to you. – rvphx May 30 '17 at 17:23
  • This does not seem to replace the D: on the Log Folder line. For example the servername is abc123D: should have been abc123d$..but as of now it remains as abc123D: – rvphx May 30 '17 at 17:39
  • Thanks @rvphx - I just added a note to your question. I'll update the pattern and replacement criteria to reflect the updated requirement. – Paurian May 30 '17 at 17:40
  • Thank you. Still seeing the same results on email though: Log Folder: \\ServerName1234D:\Apps\SSIS\Logs\ – rvphx May 30 '17 at 18:07
  • Thanks - I went ahead and wrote up a class then tested it. The beginning line anchor might be your issue. See the new answer. – Paurian May 30 '17 at 18:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145489/discussion-between-paurian-and-rvphx). – Paurian May 30 '17 at 18:30
0

Something along these lines will probably get you there:

        string x = "Server Start Time: 3/21/2017 7:25:33 AM" +
                    "Server End Time: 3 / 21 / 2017 7:27:39 AM" +
                    "Total Run Time: 00:02:06.9402516" +
                    @"Log Folder: \\ServerNameD:\Apps\SSIS\Logs\" + Environment.NewLine +

                    "UID: 2 - ";

        int lastNewlinw = x.LastIndexOf(Environment.NewLine);
        string beginning = x.Substring(0, lastNewlinw).Replace("D:", @"\d$");
        string result = string.Concat(beginning, x.Substring(lastNewlinw + 1, x.Length - beginning.Length));
MORCHARD
  • 263
  • 2
  • 12