0

I'm doing an application that will read a file and parse all the script name that this file execute. I want to be able to stuck each script name in different variable so once the program is done reading the first file he can go read the second file that would be stock in my first dynamic variable. Since i'm reading a file line by line when i find a script name to execute i display it in a richtextbox but how can i also add it to a dynamic variable.

Example First file executed contains:

blablabla
@Exec=test.sqi
blablabla
@Exec=test2.sqi

At the end of my file reading i want to be able to have variable like those: ScriptName1 = test.sqi ScriptName2 = test2.sqi

 while ((Line = file.ReadLine()) != null)
        {

            if (Line.Contains(FirstString))
            {

                int Pos1 = Line.IndexOf(FirstString) + FirstString.Length;
                int Pos2 = Line.IndexOf(LastString);
                FinalString = Line.Substring(Pos1, Pos2 - Pos1);
                FinalStringTrimed = FinalString.Trim('(',')');
                Extension = FinalStringTrimed.Substring(0,3);
                Extension = "." + Extension ;
                FileName = FinalStringTrimed.Substring(4, FinalStringTrimed.Length - 4);
                FullFileName = FileName + Extension;

                richTextBoxProcess.AppendText(FullFileName);
                richTextBoxProcess.AppendText(Environment.NewLine);

                MessageBox.Show(FullFileName);    

            }


            counter++;
        }

        file.Close();

    }

Thank for the help!

2 Answers 2

1

Create simple array or better List<string> for that purpose like this:

List<string> Scripts = new List<string>();
Scripts.Add("firstScriptName");
Scripts.Add("secondScriptName");
Scripts.Add("thirdScripName");

and you can access it like Scripts[0] for firsScriptName or irritate through it with foreach(string s in Scripts) or do transform it to simple array like string[] arrayScripts = Scripts.ToArray()

Sign up to request clarification or add additional context in comments.

2 Comments

thanks its working properly however i have another question maybe you guide me the second file i'm going throught is an HTML file and it seems like the If Contains() method is not working for parsing this file when i try we another type of file like an SQL it work fine ..any idea why is that ?thanks
Seems like i need to use an HTML parser neverming i will read on that thanks again
1
var counter = 1;
var scriptName = "ScriptName";
var dict = new Dictionary<string, string>();

        while ((Line = file.ReadLine()) != null)
        {
            var cols = Line.Split('=');

            if(cols.Length < 2) {
                continue;
            }

            var value = cols[1];
            dict[string.Format("{0}{1}", scriptName, counter)] = value;

            counter++;
        }

        foreach(var keyValue in dict) {
            Console.WriteLine(string.Format("{0} {1}", keyValue.Key, keyValue.Value));
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.