Parsing out the filepath of a text file

I have a text file which contains a number of different columns, the last column is a file path.  The file path may contain one or more filepaths, delimited with a Pipe.  How could I parse the filepath and retain the data?

For example the data looks like:

Column1, Column2, Column3, C:\Folder\filepath.pdf | C:\Folder\filepath2.tif | C:\Folder\filepath3.tif
Column1, Column2, Column3, C:\Folder\filepath5.pdf

Should be:

Column1, Column2, Column3, C:\Folder\filepath.pdf
Column1, Column2, Column3, C:\Folder\filepath2.tif
Column1, Column2, Column3, C:\Folder\filepath3.tif
Column1, Column2, Column3, C:\Folder\filepath5.pdf

Any thoughts on accomplishing this?
lm1189Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dustockCommented:
Here is some code that will do what you want

        private void ReadFile(String FilePath)
        {
            String strRecord;
            String[] strFields;

            StreamReader rd = new StreamReader(FilePath);
            while (rd.Peek() > 0)
            {
                strRecord = rd.ReadLine();

                strFields = strRecord.Split(",".ToCharArray());

                MessageBox.Show(strFields[3].ToString());
            }
        }

Open in new window

dustockCommented:
If you need to split the file paths with the pipes you can do this

String[] strPaths = strFields [3].Split("|".ToCharArray());

Open in new window


And then just loop through the file paths to do what you want

        private void ReadFile(String FilePath)
        {
            String strRecord;
            String[] strFields;

            StreamReader rd = new StreamReader(FilePath);
            while (rd.Peek() > 0)
            {
                strRecord = rd.ReadLine();

                strFields = strRecord.Split(",".ToCharArray());

                //MessageBox.Show(strFields[3].ToString());

                String[] strPaths = strFields[3].Split("|".ToCharArray());

                for (Int32 i = 0; i < strPaths.Count(); i++)
                {
                    MessageBox.Show(strPaths[i].ToString());
                }
            }
        }

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Try something like this out:
string[] values;
string lastColumn;
string fileName = @"C:\Users\mikes\Documents\SomeFile.txt";
List<string> lines = new List<string>();
foreach(string line in File.ReadLines(fileName))
{
	values = line.Split(new string[] { ", "}, StringSplitOptions.None);
	lastColumn = values.Last();
	foreach(string path in lastColumn.Split(new string[] { " | " }, StringSplitOptions.None))
	{
		values[values.Length - 1] = path;
		lines.Add(String.Join(", ", values));
	}
}
File.WriteAllLines(fileName, lines);
MessageBox.Show("Done!");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Miguel OzSoftware EngineerCommented:
This is an improved version of previous two answers and assume that paths are located at the end fo the line:
        public static List<string> GetFilePathsFromCSVFile(string filename)
        {
            List<string> result = new List<string>();
            using (StreamReader r = new StreamReader(filename))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    var items = line.Split(',');
                    var paths = items.Last().Split('|');
                    result.AddRange(paths);
                }
            }
            return result;
        }

Open in new window

Notes:
1) Using guarantees we close the file reader properly.
2) No need for ToCharArray().
3) No hard coded path location.
4) All paths are returned from above method.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
My solution above assumed you wanted to expand the paths into their own independent lines while keeping (and duplicating) the other columns in the line.   It then overwrites the original file.  This was a literal interpretation of your posted desired output.   =)
Miguel OzSoftware EngineerCommented:
@Mike: My apologies I did not see your answer. I was looking as Dustok's answer when I answer this. You answer  is correct.
Vitor MontalvãoMSSQL Senior EngineerCommented:
lm1189, do you still need help with this question?
lm1189Author Commented:
Great answer, this worked perfect.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.