Search Dictionary for partial file name

Hi,

I have a series of PDF files called “LASTNAME, FIRSTNAME - REPORT.PDF”

I have “LASTNAME, FIRSTNAME” loaded in a data table, and assigned to Dictionary<String,String> variable.

i’m trying to look up the file name against the dictionary using this: but the “- REPORT” in the name is obviously causing problems.

how can i remove the extra words from the search or is there a better way? I’m trying to set a PDF password using a corresponding code in the data table.

dict2.ContainsKey(System.IO.Path.GetFileNameWithoutExtension(item))

Hi,

Can you try the following expression?

 dict.Keys.Any(Function(s) System.IO.Path.GetFileNameWithoutExtension(item).StartsWith(s))

However, there might be 2 or more matches. For example, “John Smith” and “John Smith Jr”
It might be better to check count of matches as the following.

arrResult = dict.Keys.Where(Function(s) System.IO.Path.GetFileNameWithoutExtension(item).StartsWith(s)).ToArray

Then check arrResult.Count=1 or not

Regards,

Thanks Yoichi,
There will certainly be many matches as there will be “Lastname, Firstname Middlename - report”

And identical last and first but different middle name. is there a way to remove the last N characters from the string? the “- Report” is consistent for ever file name.

Actually i just checked. There are only 5 duplicates… I will simply remove them. Easy.

Hi,

The following will work. Can you try this?

System.Text.RegularExpressions.Regex.Replace(yourString,“- Report$”,“”)

OR

System.Text.RegularExpressions.Regex.Replace(yourString,“- Report$”,“”,System.Text.RegularExpressions.RegexOptions.IgnoreCase)

Regards,

Hi @JamesC

You can directly use a search pattern in your DIrectory.GetFiles that will give you required pdf Directory.GetFiles(“”,“" + FirstName + ".pdf”) . Include as many as you need.

Cheers