Function to Find and Replace in C#
-
Hi All, I've been putting together a short program too automatically to find and replace a string within all files of a directory. For my first set of replacements I want to replace: • “>” with “>” • “<” with “<” Program.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Program
{
static void Main(string[] args)
{//Console.WriteLine("Please Enter the directory of XML Documents:\\n"); //string userDir = Console.ReadLine(); string userDir = "C:\\\\xmlreformat"; try { string\[\] filePaths = Directory.GetFiles(userDir); foreach (string i in filePaths) { // Call reformat operation Reformat.Replace(i); } Console.WriteLine("Operation Succeeded \\n \\n Press Any Key to Exit"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Directory Not Found:" + e.Message); Console.WriteLine("\\n \\n Press Any Key to Exit"); Console.ReadKey(); } } }
}
Reformat.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Reformat
{public static void Replace(string FName) { StreamReader File = new StreamReader(FName); // create new streamreader string fileContent = File.ReadToEnd(); // create new string, read file into string File.Close(); StreamWriter NewFile = new StreamWriter(FName); // create new stream writer string fileContentCopy = fileContent; string istring1 = ">"; string ostring1 = ">"; string istring2 = "<"; string ostring2 = "<"; fileContentCopy.Replace(istring1, ostring1); fileContentCopy.Replace(istring2, ostring2); fileContent = fileContentCopy; NewFile.Write(fileContent); } }
}
However when I run the program it doesnt seem to work at all. Any ideas? Thanks, George
-
Hi All, I've been putting together a short program too automatically to find and replace a string within all files of a directory. For my first set of replacements I want to replace: • “>” with “>” • “<” with “<” Program.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Program
{
static void Main(string[] args)
{//Console.WriteLine("Please Enter the directory of XML Documents:\\n"); //string userDir = Console.ReadLine(); string userDir = "C:\\\\xmlreformat"; try { string\[\] filePaths = Directory.GetFiles(userDir); foreach (string i in filePaths) { // Call reformat operation Reformat.Replace(i); } Console.WriteLine("Operation Succeeded \\n \\n Press Any Key to Exit"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Directory Not Found:" + e.Message); Console.WriteLine("\\n \\n Press Any Key to Exit"); Console.ReadKey(); } } }
}
Reformat.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Reformat
{public static void Replace(string FName) { StreamReader File = new StreamReader(FName); // create new streamreader string fileContent = File.ReadToEnd(); // create new string, read file into string File.Close(); StreamWriter NewFile = new StreamWriter(FName); // create new stream writer string fileContentCopy = fileContent; string istring1 = ">"; string ostring1 = ">"; string istring2 = "<"; string ostring2 = "<"; fileContentCopy.Replace(istring1, ostring1); fileContentCopy.Replace(istring2, ostring2); fileContent = fileContentCopy; NewFile.Write(fileContent); } }
}
However when I run the program it doesnt seem to work at all. Any ideas? Thanks, George
GrgBalden wrote:
fileContentCopy.Replace(istring1, ostring1); fileContentCopy.Replace(istring2, ostring2);
String.Replace function returns an altered string, it does not change the instance itself. You need to use...
fileContentCopy = fileContentCopy.Replace(istring1, ostring1);
fileContentCopy = fileContentCopy.Replace(istring2, ostring2);I would also like to add that reading a whole file to a string, in my opinion, is not a good thing. I would prefer to read a character at a time and match it to a set of keywords and replace any matches.
Life goes very fast. Tomorrow, today is already yesterday.
-
GrgBalden wrote:
fileContentCopy.Replace(istring1, ostring1); fileContentCopy.Replace(istring2, ostring2);
String.Replace function returns an altered string, it does not change the instance itself. You need to use...
fileContentCopy = fileContentCopy.Replace(istring1, ostring1);
fileContentCopy = fileContentCopy.Replace(istring2, ostring2);I would also like to add that reading a whole file to a string, in my opinion, is not a good thing. I would prefer to read a character at a time and match it to a set of keywords and replace any matches.
Life goes very fast. Tomorrow, today is already yesterday.
Hi, Thanks for the help I've added your suggestion and it seems to be working; I'm now trying to change the file extention of the files in the directory from .txt to .xml The text files already have the xml, i just need to change the file extention? Any ideas? I tried to impliment an Ext Changer class (last in the list) but doesnt seem to work? Any provide any sample code? Program.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Program
{
static void Main(string[] args)
{//Console.WriteLine("Please Enter the directory of XML Documents:\\n"); //string userDir = Console.ReadLine(); string userDir = "C:\\\\xmlreformat"; try { string\[\] filePaths = Directory.GetFiles(userDir); foreach (string i in filePaths) { // Call reformat operation Reformat.Replace(i); //ExtChanger.WriteFileName(i, userDir); } Console.WriteLine("Operation Succeeded \\n \\n Press Any Key to Exit"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Directory Not Found:" + e.Message); Console.WriteLine("\\n \\n Press Any Key to Exit"); Console.ReadKey(); } } }
}
Reformat.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Reformat
{public static void Replace(string FName) { StreamReader File = new StreamReader(FName); // create new streamreader string fileContent = File.ReadToEnd(); // create new string, read file into string File.Close(); StreamWriter NewFile = new StreamWriter(FName); // create new stream writer string fileContentCopy = fileContent; string istring1 = ">"; string ostring1 = ">"; string istring2 = "<"; string ostring2 = "<"; fileContentCopy = fileContentCopy.Replace(istring1, ostring1); fileContentCopy = fileContentCopy.Replace(istring2, ostring2); fileContent = fileContentCopy; N
-
Hi All, I've been putting together a short program too automatically to find and replace a string within all files of a directory. For my first set of replacements I want to replace: • “>” with “>” • “<” with “<” Program.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Program
{
static void Main(string[] args)
{//Console.WriteLine("Please Enter the directory of XML Documents:\\n"); //string userDir = Console.ReadLine(); string userDir = "C:\\\\xmlreformat"; try { string\[\] filePaths = Directory.GetFiles(userDir); foreach (string i in filePaths) { // Call reformat operation Reformat.Replace(i); } Console.WriteLine("Operation Succeeded \\n \\n Press Any Key to Exit"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Directory Not Found:" + e.Message); Console.WriteLine("\\n \\n Press Any Key to Exit"); Console.ReadKey(); } } }
}
Reformat.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Reformat
{public static void Replace(string FName) { StreamReader File = new StreamReader(FName); // create new streamreader string fileContent = File.ReadToEnd(); // create new string, read file into string File.Close(); StreamWriter NewFile = new StreamWriter(FName); // create new stream writer string fileContentCopy = fileContent; string istring1 = ">"; string ostring1 = ">"; string istring2 = "<"; string ostring2 = "<"; fileContentCopy.Replace(istring1, ostring1); fileContentCopy.Replace(istring2, ostring2); fileContent = fileContentCopy; NewFile.Write(fileContent); } }
}
However when I run the program it doesnt seem to work at all. Any ideas? Thanks, George
System.Web.HttpUtility.HtmlEncode
andSystem.Web.HttpUtility.HtmlDecode
-
System.Web.HttpUtility.HtmlEncode
andSystem.Web.HttpUtility.HtmlDecode
-
Hi, Thanks for the help I've added your suggestion and it seems to be working; I'm now trying to change the file extention of the files in the directory from .txt to .xml The text files already have the xml, i just need to change the file extention? Any ideas? I tried to impliment an Ext Changer class (last in the list) but doesnt seem to work? Any provide any sample code? Program.CS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Program
{
static void Main(string[] args)
{//Console.WriteLine("Please Enter the directory of XML Documents:\\n"); //string userDir = Console.ReadLine(); string userDir = "C:\\\\xmlreformat"; try { string\[\] filePaths = Directory.GetFiles(userDir); foreach (string i in filePaths) { // Call reformat operation Reformat.Replace(i); //ExtChanger.WriteFileName(i, userDir); } Console.WriteLine("Operation Succeeded \\n \\n Press Any Key to Exit"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Directory Not Found:" + e.Message); Console.WriteLine("\\n \\n Press Any Key to Exit"); Console.ReadKey(); } } }
}
Reformat.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace XMLReformat
{
class Reformat
{public static void Replace(string FName) { StreamReader File = new StreamReader(FName); // create new streamreader string fileContent = File.ReadToEnd(); // create new string, read file into string File.Close(); StreamWriter NewFile = new StreamWriter(FName); // create new stream writer string fileContentCopy = fileContent; string istring1 = ">"; string ostring1 = ">"; string istring2 = "<"; string ostring2 = "<"; fileContentCopy = fileContentCopy.Replace(istring1, ostring1); fileContentCopy = fileContentCopy.Replace(istring2, ostring2); fileContent = fileContentCopy; N
Try using the FileInfo class (in System.IO)...
FileInfo fi = new FileInfo("");//use actual filepath
string NewFN = fi.Name.SubString(0, fi.Name.Length - fi.Extension.Length);//Get file name without extension
NewFN += ".XML";//then use MoveTo to effectively rename the file
fi.MoveTo(fi.DirectoryName + NewFN, true);
//true is used to specify the target file will be overwrote if it already exists.That should do the trick for you ;)
Life goes very fast. Tomorrow, today is already yesterday.
-
Thanks for the pointer, I was hoping that someone would know how to change a file extention by changing the file extention using a string? Any more ideas?
System.IO.Path.GetFileNameWithoutExtension
andSystem.IO.File.Move