Open txt file and replace text in the first two lines (C#)

user1848210

I am writing this program that allows me to generate txt files that with an incremental number, however, i want each file serial number to be writting inside the txt file itself.

For example: I generated 3 files, Mytext-000001.txt, Mytext-000002.txt, Mytext-000003.txt, and each file first line contains "Hello 000000" and the second line contains "My number is 000000", now i want to change each txt file to contain "Hello " + the incremental number that it is named with.

So the output of each file will be:

Mytext-000001.txt,
Hello 000001
My number is 000001

Mytext-000002.txt,
Hello 000002
My number is 000002


Mytext-000003.txt,
Hello 000002
My number is 000003

My Code

string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\path.txt";
string pth_input = null;
string pth_output = null;

using (StreamReader sx = File.OpenText(path))
{
    pth_input = sx.ReadLine();
    pth_output = sx.ReadLine();
}


Console.WriteLine("Number of Files?");
string number_of_files = Console.ReadLine();
int get_number_of_files = Int32.Parse(number_of_files) + 1;

string PathFiletoCopy = pth_input;
string Extension = System.IO.Path.GetExtension(PathFiletoCopy);
string PartialNewPathFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(PathFiletoCopy), System.IO.Path.GetFileNameWithoutExtension(PathFiletoCopy) + "-");

for (int i = 1; i < get_number_of_files; i++)
{
    System.IO.File.Copy(PathFiletoCopy, PartialNewPathFile + i.ToString("D6") + Extension);
}
string[] txtfiles = Directory.GetFiles(pth_output, "*.txt");
foreach (var file in txtfiles)
{
    string get_file_counter = System.IO.Path.GetDirectoryName(file.Substring(7,6));
    FileStream fs = new FileStream(file, FileMode.Append, FileAccess.Write);
    FileStream fi = new FileStream(file, FileMode.Open, FileAccess.Read);
    using (StreamReader reader = new StreamReader(fi))
    {
        using (StreamWriter writer = new StreamWriter(fs))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {

             string replace_line_one = line.Replace("Hello 000001","Hello"+ "["+get_file_counter+"]");
             string replace_line_two = line.Replace("My number is 000001", "My number is" + "[" + get_file_counter + "]");

            }
            writer.Close();
        } reader.Close();

    } 
}
Console.Read();

I hope you can help

Appreciate your help guys

Abion47
string[] files = Directory.GetFiles(directoryPath, "*.txt");
Regex regex = new Regex("\\d+(?=\\.txt)");

foreach (var file in files)
{
    string[] lines = File.ReadAllLines(file);
    string number = regex.Match(Path.GetFileName(file)).Value;

    lines[0] = "Hello " + number;
    lines[1] = "My number is " + number;

    File.WriteAllLines(file, lines);
}

Regex makes this solution nonspecific.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace multiple lines in text file

From Dev

C# overwrite first few lines of a text file with constant time

From Dev

C# overwrite first few lines of a text file with constant time

From Dev

Replace first few lines with first few lines from other file

From Dev

replace text lines between two characters

From Dev

Writing text to txt file in python on new lines?

From Dev

BAT file to replace string in two lines

From Dev

Read all lines from TXT file and search only in every FIRST string of each line C#

From Dev

Read all lines from TXT file and search only in every FIRST string of each line C#

From Dev

How to find and replace multiple lines in text file?

From Dev

Replace with lines number in specific line of a text file

From Dev

How to replace text in a txt file with python?

From Dev

How to replace particular text in the txt file?

From Dev

How to replace text and symbols in a txt file?

From Dev

delete a line and replace with new text in a txt file

From Dev

How to copy lines between two strings to a new text file matching first string fully and second string partially

From Dev

By pass the first two lines of csv file

From Dev

Remove First n Lines of a Large Text File

From Dev

Write text into first lines of every file

From Dev

Remove First n Lines of a Large Text File

From Dev

How to sort lines of a text file by first word?

From Dev

extract 5 first lines of a text file to a variable

From Dev

Read two lines at a time from a .txt file - C++ getline/ streams?

From Dev

replace lines in a text file from another text file

From Dev

Writing lines to .txt file c++

From Dev

C function that counts lines in txt file

From Dev

Writing lines to .txt file c++

From Dev

Switching two lines in a text file in Java

From Dev

Replace the first line with the longest java text file

Related Related

  1. 1

    Replace multiple lines in text file

  2. 2

    C# overwrite first few lines of a text file with constant time

  3. 3

    C# overwrite first few lines of a text file with constant time

  4. 4

    Replace first few lines with first few lines from other file

  5. 5

    replace text lines between two characters

  6. 6

    Writing text to txt file in python on new lines?

  7. 7

    BAT file to replace string in two lines

  8. 8

    Read all lines from TXT file and search only in every FIRST string of each line C#

  9. 9

    Read all lines from TXT file and search only in every FIRST string of each line C#

  10. 10

    How to find and replace multiple lines in text file?

  11. 11

    Replace with lines number in specific line of a text file

  12. 12

    How to replace text in a txt file with python?

  13. 13

    How to replace particular text in the txt file?

  14. 14

    How to replace text and symbols in a txt file?

  15. 15

    delete a line and replace with new text in a txt file

  16. 16

    How to copy lines between two strings to a new text file matching first string fully and second string partially

  17. 17

    By pass the first two lines of csv file

  18. 18

    Remove First n Lines of a Large Text File

  19. 19

    Write text into first lines of every file

  20. 20

    Remove First n Lines of a Large Text File

  21. 21

    How to sort lines of a text file by first word?

  22. 22

    extract 5 first lines of a text file to a variable

  23. 23

    Read two lines at a time from a .txt file - C++ getline/ streams?

  24. 24

    replace lines in a text file from another text file

  25. 25

    Writing lines to .txt file c++

  26. 26

    C function that counts lines in txt file

  27. 27

    Writing lines to .txt file c++

  28. 28

    Switching two lines in a text file in Java

  29. 29

    Replace the first line with the longest java text file

HotTag

Archive