Hi,

I am pretty new to c# presently. I have a text file called tc.txt (comman separated) with the following content below:


T001,Dirks,c:\dirks\001.tif
T001,Dirks,c:\dirks\002.tif
T002,Matt,c:\matt\003.tif
T002,Matt,c:\matt\004.tif
T003,Ted,c:\Ted\005.tif
T003,Ted.c:\Ted\006.tif

When the file is read and written to another text file called output.txt (comma seperated). every line within the same folder should be written as follow;

T001,Dirks,c:\dirks\001.tif,c:\dirks\002.tif
T002,Matt,c:\matt\003.tif,c:\matt\004.tif
T003,Ted,c:\Ted\005.tif,c:\Ted\006.tif

Any help will be appreciated.

Dani AI

Generated

A reliable approach is to group rows by the ID and collect all paths for that ID, then write one output line per group. The sample input contains a malformed line (T003,Ted.c:\Ted\006.tif — missing comma) so any solution should validate each line, log or skip malformed rows, and handle variable numbers of files per ID.

using System;
using System.Collections.Generic;
using System.IO;

var groups = new Dictionary<string, List<string>>();
var names = new Dictionary<string, string>();

foreach (var raw in File.ReadLines("tc.txt"))
{
    var line = raw.Trim();
    if (string.IsNullOrEmpty(line)) continue;

    // split into at most 3 pieces: id, name, path
    var parts = line.Split(new[] { ',' }, 3);
    if (parts.Length != 3)
    {
        Console.Error.WriteLine("Skipping malformed line: " + line);
        continue;
    }

    var id = parts[0].Trim();
    var name = parts[1].Trim();
    var path = parts[2].Trim();

    if (!groups.TryGetValue(id, out var list))
    {
        list = new List<string>();
        groups[id] = list;
        names[id] = name;
    }
    else if (!string.Equals(names[id], name, StringComparison.OrdinalIgnoreCase))
    {
        Console.Error.WriteLine($"Name mismatch for {id}: '{names[id]}' vs '{name}'");
    }

    list.Add(path);
}

using (var w = new StreamWriter("output.txt"))
{
    foreach (var id in groups.Keys)
        w.WriteLine($"{id},{names[id]},{string.Join(",", groups[id])}");
}

Notes: use the Split(..., 3) trick to avoid splitting any accidental commas in the path. Consider deduplication with a HashSet<string> if the same filename may repeat, and use Microsoft.VisualBasic.FileIO.TextFieldParser (or a CSV library) if fields can be quoted or contain commas. This implements the grouping idea suggested by and avoids the fragile "concatenate every two lines" approach mentioned by ; it also explicitly handles the malformed line from 's sample.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

This is pretty straight forward string manipulation. Read each line into a buffer then, just concatenate every two lines. chop out the parts you don't need. Every two lines could be accounted for using the mod operator.

It could be better to keep lines in Dictionary<string, Collection<string>), where T001, T002 etc is key, and string with filename is a part of value collection. Advantage of this way is that it is OK if source strings are mixed up (T001, T003, again T001, T002 etc), and every T... may contain any number of lines, but not only two.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.