Link to home
Create AccountLog in
Avatar of lm1189
lm1189

asked on

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?
Avatar of dustock
dustock
Flag of United States of America image

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

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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
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.   =)
@Mike: My apologies I did not see your answer. I was looking as Dustok's answer when I answer this. You answer  is correct.
lm1189, do you still need help with this question?
Avatar of lm1189
lm1189

ASKER

Great answer, this worked perfect.