0

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

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • Could you provide us with the file you are using? Could you also explain what you mean by " 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." - I'm not sure I follow. – Faraday Sep 10 '13 at 09:55
  • How do I add a file to this comment? Explanation: – user2763236 Sep 10 '13 at 10:03
  • @Vijay how do I add a file to this comment? Explanation: I need to be able to change the nr of the treatments im doing. So I have this data in text files. And line 32 have "Treatment";"nr"; And for each experiment I get 96 text files. which ranges from 1-96 in the "nr"; I need to be able to start from what number i want instead of 1, so for example if I start with 200 in the first file I would get 296 in the last file. – user2763236 Sep 10 '13 at 10:09
  • Throw the contents of your file in pastebin and put a link in your question. :) – Faraday Sep 10 '13 at 10:25

1 Answers1

1

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);
            }
        }
    }
Bolu
  • 8,696
  • 4
  • 38
  • 70
  • The files are sorted by name, my program just needs a user input what integer to start on. So finding the number in each string and adding by 100 is not what im looking for, I think. I need to be able to start from where I want like for example 90 then the last file would get 186; – user2763236 Sep 10 '13 at 10:13
  • can't you change 100 in the above code to your user's input?? – Bolu Sep 10 '13 at 10:14
  • Yes, me and my thick skull. Thanks alot ;D it works perfectly. @Bolu. You rock! – user2763236 Sep 10 '13 at 10:26