2

I have data table containing one column as FilePath.

FilePath
D:\New folder\link.txt
D:\New folder\SharepointMigration(Work Schedule).xls
D:\New folder\my2\link.txt
D:\New folder\my2\SharepointMigration(Work Schedule).xls
D:\New folder\my2\my3\New Text Document.txt
D:\New folder\Try.txt

I am filtering my data table by

DataRow[] dtRow = dataTable_FilesWithMetaData.Select("FilePath Like '" + sourcePath + "%'");

But it gives me all files and subfolder files. But i want only files not subfolders. How to write such type of filter expression..??

EDIT: I am using VS-2008 2.0 framework. The files are coming from some server in xml format and then i am populating my gridview.

2 Answers 2

2

If LINQ is available:

DataTable dt = new DataTable();

dt.Columns.Add("FilePath", typeof(string));

dt.Rows.Add(@"D:\New folder\link.txt");
dt.Rows.Add(@"D:\New folder\my2\link.txt");

string target = Path.GetDirectoryName(@"D:\New folder\");

DataRow[] rows = dt.Rows.Cast<DataRow>().Where(dr =>
    Path.GetDirectoryName(((string)dr["FilePath"])).Equals(target))
    .ToArray();

This will only return the row containing ("D:\New folder\link.txt").

In .NET 2.0 you can create a helper method, something like this:

public static DataRow[] GetRowsForDir(DataTable table, string targetDir)
{
    var result = new List<DataRow>();

    foreach (DataRow row in table.Rows)
    {
        if (Path.GetDirectoryName(((string)row["FilePath"])).Equals(targetDir))
        {
            result.Add(row);
        }
    }

    return result.ToArray();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks João Angelo, Ur helper method works..Good way to do such thing.. Thanks 1s again ;)
0

If the files are on the machine running the application, why not just do a

Directory.GetFiles(sourcePath)

for the path you want?

1 Comment

The files are coming from some server in xml format and then i am populating gridview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.