November 12, 2018 at 10:34 am
I need to replace all occurrences of 'ABCD' in .txt file with 'EFGH'.
Is it possible to do from an SSIS task? (which one?)
Likes to play Chess
November 12, 2018 at 10:38 am
VoldemarG - Monday, November 12, 2018 10:34 AMI need to replace all occurrences of 'ABCD' in .txt file with 'EFGH'.
Is it possible to do from an SSIS task? (which one?)
I'd do this in C# using a script task.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 12, 2018 at 2:00 pm
Phil Parkin - Monday, November 12, 2018 10:38 AMVoldemarG - Monday, November 12, 2018 10:34 AMI need to replace all occurrences of 'ABCD' in .txt file with 'EFGH'.
Is it possible to do from an SSIS task? (which one?)I'd do this in C# using a script task.
I tried different ways (using VB but doesn't matter...)
I tried this way (does not work). using Data Tools 2015.Const ForReading = 1
Const ForWriting = 2
Dim objFSO, objFile, strText, strNewText
objFSO = CreateObject("Scripting.FileSystemObject")
objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close()strNewText = Replace(strText, "WTOTA", "TOTAL")
objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt", ForWriting)
objFile.WriteLine(strNewText)
objFile.Close()
objFSO = Nothing
objFile = Nothing
and i also tried this way: (doesn't work either)
Imports System.IO // in imports section
thenDim filePath As String = Dts.Variables("User::FullFilePath").Value.ToString()
File.WriteAllText(filePath, File.ReadAllText(filePath).Replace("Jim", "James"))
Likes to play Chess
November 12, 2018 at 2:16 pm
I have tried this way as well but code does not compile...
See the attached too, same.
Public Sub Main()
'
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Dts.Variables("FullFilePath").Value)
Dim writer As System.IO.StreamWriter = New System.IO.StreamWriter(Dts.Variables("FullFilePath2").Value)
While Not reader.EndOfStream
Dim theLine As String = reader.ReadLine()
' Do your replacements here
theLine = theLine.Replace("WTOTA", "TOTAL")
theLine = theLine.Replace("oldValue1", "newValue1")
' etc, etc, etc.
' Write the cleansed output to the new file
writer.WriteLine(theLine)
End While
writer.Flush()
writer.Close()
'
Dts.TaskResult = ScriptResults.Success
End Sub
Likes to play Chess
November 12, 2018 at 2:59 pm
I finally got it working THIS WAY:
public void Main()
{
string fileText;
try
{
fileText = System.IO.File.ReadAllText(Dts.Variables["FullFilePath"].Value.ToString());
fileText = fileText.Replace("WTOTA", "TOTAL");
System.IO.File.WriteAllText(Dts.Variables["FullFilePath"].Value.ToString(), fileText);
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw ex;
}
finally
{
fileText = null;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Likes to play Chess
November 13, 2018 at 7:37 am
VoldemarG - Monday, November 12, 2018 2:59 PMI finally got it working THIS WAY:public void Main()
{
string fileText;
try
{
fileText = System.IO.File.ReadAllText(Dts.Variables["FullFilePath"].Value.ToString());
fileText = fileText.Replace("WTOTA", "TOTAL");
System.IO.File.WriteAllText(Dts.Variables["FullFilePath"].Value.ToString(), fileText);Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw ex;
}
finally
{
fileText = null;
}Dts.TaskResult = (int)ScriptResults.Success;
}
If you have a huge file, what does the System.IO.File.ReadAllText do to memory on the system?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 13, 2018 at 8:12 am
Jeff Moden - Tuesday, November 13, 2018 7:37 AMIf you have a huge file, what does the System.IO.File.ReadAllText do to memory on the system?
Consumes it!
Huge files need to be streamed in and out to avoid this.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 13, 2018 at 9:11 am
Phil Parkin - Tuesday, November 13, 2018 8:12 AMJeff Moden - Tuesday, November 13, 2018 7:37 AMIf you have a huge file, what does the System.IO.File.ReadAllText do to memory on the system?
Consumes it!
Huge files need to be streamed in and out to avoid this.
That's what I was reading into it. Heh.... might as well make a call to Notepad then. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply