Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing Text File 1

Status
Not open for further replies.

EzehM

Programmer
Feb 27, 2003
86
0
0
GB
Something wrong with my logic and code!
Please help!!

I have the following text file, and I want to extract the first field (directory paths) of each line, and maybe write it to another file or an arraylist.

This is the file:

c:\mfta\t;c:\mfta\g|hr-admin|c:\mfta\s;c:\mfta\v|txt;doc;xls|n|n|y|y
c:\mfta\a;c:\mfta\e|hr-finance|c:\mfta\f;c:\mfta\g|txt;doc;xls|y|y|y|y
c:\mfta\q|hr-finance|c:\mfta\f;c:\mfta\g|txt;doc;xls|y|y|y|y

This is what I expect the new file to contain:
c:\mfta\t
c:\mfta\g
c:\mfta\a
c:\mfta\e
c:\mfta\q

This is my code:

StreamReader sr = wFile.OpenText();
while ((line = sr.ReadLine()) != null)
{
//TRIM at the '|'
string[] directory = line.Split('|');
Console.WriteLine(directory[0]);

// check for multiple directories
// seperated by semi colon (;)
int found = directory[0].IndexOf(";", 0);

if (found > -1)
{
Console.WriteLine("Multiple directory");
// bug here!!
string[] dirs = directory[0].Split(';');
for(int i = 0; i < dirs.Length; i++)
Console.WriteLine(dirs[0]);
}
else
Console.WriteLine(directory[0]);
}
sr.Close();

Please help, thanks in advance



 
Replace the code from:
Code:
else
                    Console.WriteLine(dirs[0]);
to
Code:
else
                    Console.WriteLine(dirs[i])
and check whether u get expected output.

Sharing the best from my side...

--Prashant--
 
Thanks...this did the trick
for(int i = 0; i < dirs.Length; i++)
Console.WriteLine((dirs))

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top