Search a string without . character

98 Views Asked by At

I'm stuck with a search file problem.

I have a file say:

1346670589521421983450911196954093762922.nii

that is referenced to the file below:

1.3.46.670589.5.2.14.2198345091.1196954093.762922.dcm

The difference is that there are (.) placed in different positions. Does anybody know how to search for a file without the fullstop(.)? As i have to go through 300 directories all with diffrent file naming conventions i cannot just use substring to break them up. Thank you for all your help.

Cheers.

Just realised might be confusing what I described earlier.

In Summary I'm trying to use this string 1346670589521421983450911196954093762922 to look through a directory with hundreds of .dcms. That looks like 1.3.46.670589.5.2.14.2198345091.1196954093.762922.dcm. How can i search it based on 1346670589521421983450911196954093762922 string? Also the solution has to be on .net 3.5. Thank you.

2

There are 2 best solutions below

2
On BEST ANSWER

So you want to ignore the dots and the extension of the file-name?

You could use String.Replace to remove the dots, a loop or LINQ query and the Path-class:

string searchFileNoExt = Path.GetFileNameWithoutExtension("1346670589521421983450911196954093762922.nii");
var filesToProcess = Directory.EnumerateFiles(rootDir, ".*.", System.IO.SearchOption.AllDirectories)
    .Where(fn => Path.GetFileNameWithoutExtension(fn).Replace(".", "").Equals(searchFileNoExt, StringComparison.InvariantCultureIgnoreCase));
foreach (string file in filesToProcess)
    Console.WriteLine(file);
0
On
string file1 = "1346670589521421983450911196954093762922.nii";
string file2 = "1.3.46.670589.5.2.14.2198345091.1196954093.762922.dcm";
//#1 remove extension
string file1name = System.IO.Path.GetFileNameWithoutExtension(file1);
string file2name = System.IO.Path.GetFileNameWithoutExtension(file2);
//#2 remove .
string file2normalized = file2name.Replace(".", string.Empty);
//# compare
bool equal = file1name == file2normalized;