C#. Using all files in a folder, and writing a different string in a specific line

283 views Asked by At

I need your help.

The thing is that my code works, it reads all the files in a folder which are 96 text files and saves the path of each file. I then take each file and change the line number 32 in the text file which is "Treatment";"1"; nr = 1,2,3,4,5,...,96. My program will takes this string and replaces it with a different one, I change the first file for example to "Treatment";"100"; then the last file should be "Treatment";"196";

So to solve this i change the whole line with a new one. But when i write the number to the string first file is right when i start from 1, but files 2-10 are. 12,23,34,45,56,67,78,89, then it starts 2,3,4,5,6,7 from the 11-th file.

Why is this? My code is below.

I tried saving the integer as a string because I though i was somehow accesing a ASCII table. But that works the same, so my code is below any ideas?

    private void button1_Click(object sender, EventArgs e)
    {

        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {

            int start = 1;
            string strengur = "\";";
            string myString = start.ToString();

            string[] filePaths = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            //foreach (var file in Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath))
            for(int i = 0; i < 96 ; i++){

                var lines = File.ReadAllLines(filePaths[i]);
                lines[31] = "\"Treatment!!\";\"" +myString +strengur;
                File.WriteAllLines(filePaths[i], lines);
                start += 1;
                myString = start.ToString();
           }
        }
  }

Best Regards Sæþór Ólafur Pétursson

1

There are 1 answers

3
Bolu On

Display all these files in windows explorer, sort by name, and then you will see why.

To solve it, you can set your start based on each file's line31's current number, and add by 100. E.g.:

 private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string strengur = "\";";
                string[] filePaths = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach(var file in filePaths)
                {
                    var lines = File.ReadAllLines(file);
                    int currentstart = int.Parse(lines[31].Split(';')[1].Trim('\"'));
                    lines[31] = "\"Treatment!!\";\"" + (currentstart+100).ToString() + strengur;
                    File.WriteAllLines(file, lines);
                }
            }
        }

Edit based on your comment:

 private void button1_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            int start = 100; //set this to your user's input
            string strengur = "\";";
            string[] filePaths = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            foreach(var file in filePaths)
            {
                var lines = File.ReadAllLines(file);
                int currentstart = int.Parse(lines[31].Split(';')[1].Trim('\"'));
                lines[31] = "\"Treatment!!\";\"" + (currentstart+start-1).ToString() + strengur;
                File.WriteAllLines(file, lines);
            }
        }
    }