Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 570 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find and replace text in a file

#1
My code so far

StreamReader reading = File.OpenText("test.txt");
string str;
while ((str = reading.ReadLine())!=null)
{
if (str.Contains("some text"))
{
StreamWriter write = new StreamWriter("test.txt");
}
}

I know how to find the text, but I have no idea on how to replace the text in the file with my own.
Reply

#2
You're going to have a hard time writing to the same file you're reading from. One quick way is to simply do this:

File.WriteAllText("test.txt", File.ReadAllText("test.txt").Replace("some text","some other text"));

You can lay that out better with

string str = File.ReadAllText("test.txt");
str = str.Replace("some text","some other text");
File.WriteAllText("test.txt", str);
Reply

#3
Read all file content. Make a replacement with `String.Replace`. Write content back to file.

string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);
Reply

#4
It is likely you will have to pull the text file into memory and then do the replacements. You will then have to overwrite the file using the method you clearly know about. So you would first:

// Read lines from source file.
string[] arr = File.ReadAllLines(file);

YOu can then loop through and replace the text in the array.

var writer = new StreamWriter(GetFileName(baseFolder, prefix, num));
for (int i = 0; i < arr.Length; i++)
{
string line = arr[i];
line.Replace("match", "new value");
writer.WriteLine(line);
}

this method gives you some control on the manipulations you can do. Or, you can merely do the replace in one line

File.WriteAllText("test.txt", text.Replace("match", "new value"));

I hope this helps.
Reply

#5
> This code Worked for me



- //-------------------------------------------------------------------
// Create an instance of the Printer
IPrinter printer = new Printer();

//----------------------------------------------------------------------------
String path = @"" + file_browse_path.Text;
// using (StreamReader sr = File.OpenText(path))

using (StreamReader sr = new System.IO.StreamReader(path))
{

string fileLocMove="";
string newpath = Path.GetDirectoryName(path);
fileLocMove = newpath + "\\" + "new.prn";



string text = File.ReadAllText(path);
text= text.Replace("<REF>", reference_code.Text);
text= text.Replace("<ORANGE>", orange_name.Text);
text= text.Replace("<SIZE>", size_name.Text);
text= text.Replace("<INVOICE>", invoiceName.Text);
text= text.Replace("<BINQTY>", binQty.Text);
text = text.Replace("<DATED>", dateName.Text);

File.WriteAllText(fileLocMove, text);



// Print the file
printer.PrintRawFile("Godex G500", fileLocMove, "n");
// File.WriteAllText("C:\\Users\\Gunjan\\Desktop\\new.prn", s);
}
Reply

#6
This is how I did it with a large (50 GB) file:

I tried 2 different ways: the first, reading the file into memory and using Regex Replace or String Replace. Then I appended the entire string to a temporary file.

The first method works well for a few Regex replacements, but Regex.Replace or String.Replace could cause out of memory error if you do many replaces in a large file.

The second is by reading the temp file line by line and manually building each line using StringBuilder and appending each processed line to the result file. This method was pretty fast.


static void ProcessLargeFile()
{
if (File.Exists(outFileName)) File.Delete(outFileName);

string text = File.ReadAllText(inputFileName, Encoding.UTF8);

// EX 1 This opens entire file in memory and uses Replace and Regex Replace --> might cause out of memory error

text = text.Replace("</text>", "");

text = Regex.Replace(text, @"\<ref.*?\</ref\>", "");

File.WriteAllText(outFileName, text);




// EX 2 This reads file line by line

if (File.Exists(outFileName)) File.Delete(outFileName);

using (var sw = new StreamWriter(outFileName))
using (var fs = File.OpenRead(inFileName))
using (var sr = new StreamReader(fs, Encoding.UTF8)) //use UTF8 encoding or whatever encoding your file uses
{
string line, newLine;

while ((line = sr.ReadLine()) != null)
{
//note: call your own replace function or use String.Replace here
newLine = Util.ReplaceDoubleBrackets(line);

sw.WriteLine(newLine);
}
}
}

public static string ReplaceDoubleBrackets(string str)
{
//note: this replaces the first occurrence of a word delimited by [[ ]]

//replace [[ with your own delimiter
if (str.IndexOf("[[") < 0)
return str;

StringBuilder sb = new StringBuilder();

//this part gets the string to replace, put this in a loop if more than one occurrence per line.
int posStart = str.IndexOf("[[");
int posEnd = str.IndexOf("]]");
int length = posEnd - posStart;


// ... code to replace with newstr


sb.Append(newstr);

return sb.ToString();
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through