Reputation:
How to remove a path in pathList if the file in the path already exist?
I got a List path which consist of many string of paths to some files. What I wonder is that how can I remove a path in the path list if the file is duplciated in another directory path?
For example:
/directory/data/testing/testdata.csv
/directory/data/test123/testdata.csv
/directory/data/test123/testdata3.csv?
/directory/data/testing/testdata1.csv?
/directory/data/test123/testdata2.csv?
AS you see, both files are identical in another directory, but since I already added the first line to the path list, I want to skip the second like because the file already exist in the path list.
How can I achieve this?
Upvotes: 0
Views: 100
Reputation: 1382
Try with next code:
//lspath is the list with all paths
var dataDistinc = lspath.Select(x =>
{
//get all files with .csv
if (Regex.IsMatch(x.Split('/').Last(), ".csv") || Regex.IsMatch(x.Split('/').Last(), ".csv?"))
{
var buff = x.Split('/').Last();
//get the path files with the same files
var z = lspath.Select(y => {
if(y.Contains(buff)){
return y;
}
return null;
}).Where(i => i != null).FirstOrDefault();
//return the first
return z;
//return z.Select(j => j != null ? j : null);
};
return null;
}).Distinct();
Upvotes: 0
Reputation: 216293
You can GroupBy your list using the Path.GetFileName, then on each group returned by the GroupBy select only the first item in the list
List<string> paths = new List<string>
{
"/directory/data/testing/testdata.csv",
"/directory/data/test123/testdata.csv",
"/directory/data/test123/testdata3.csv",
"/directory/data/otherfolder/testdata3.csv",
"/directory/data/testing/testdata1.csv",
"/directory/data/testdata1.csv",
"/directory/data/test123/testdata2.csv"
"/directory/data/myfolder/testdata2.csv"
};
paths = paths.GroupBy(p => Path.GetFileName(p))
.Select(x => x.First()).ToList();
foreach(string s in paths)
Console.WriteLine(s);
Output is:
/directory/data/testing/testdata.csv
/directory/data/test123/testdata3.csv
/directory/data/testing/testdata1.csv
/directory/data/test123/testdata2.csv
Upvotes: 1