Search for files by extension

Asked

Viewed 675 times

5

In a Windows Forms application I would like to know how to search all the files that exist and contain the word teste in the file extension. For example, when opening a folder in Windows and typing *.teste* in the search field will be returned all files containing that word in its extension regardless if it is .teste1, .teste2...

 string Diretorio = @"C:\teste\";
 string Arquivo = Diretorio + Codigo.Trim() + "E" + "\" + Data;

 if (File.Exists(Arquivo + ".teste"))
 {
     Retorno = true;
 }

In the code fragment above, the word testewas set to a file extension. However if I have the file with the extension .teste1 or .teste2 the file will no longer be found.

  • type a LIKE in the extension?

  • 1

    Yeah, like a like

  • but I believe it is not possible, exists checks the existence or not of the specified file only

2 answers

7


You can take all files from the directory and see if the extensions contain what you want:

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\teste\");

bool retorno;

string nomeArquivo = "MyFile";

int count = 0;
foreach (FileInfo file in directoryInfo.GetFiles())
{
    if (file.Name == nomeArquivo && file.Extension.Contains(".ok"))
        retorno = true;
}

In this other option below is checked if there is a file with that particular name and that contains in the extension the word "ok", if it exists, the count returns greater than 0 and the return variable will be equal true

bool retorno = directoryInfo.GetFiles().Where(p => p.Extension.Contains(".ok") && p.Name == nomeArquivo).ToList().Count > 0; 
  • And in that case, how could I possibly return only the last file?

  • It depends, this last file will be the last created? if yes, has file has a property has the field CreationTime, you can use it, check which one is closer to today

  • @Moisolas, I don’t understand, you’re describing different problems now. I edited the answer, in it I added double check, both by name, and by extension. It uses the Contais it will take everything that has "test", be it "testeok" or "teste1", if you want to exact that extension, then it would be the case to use the ==

  • But you just need to confirm if it’s been printed, regardless of the amount of times or need to do something with the amount of times it’s been printed?

  • 2

    @Moisolas, I’ve made an adjustment, make sure that’s what you want.

3

Just complementing @Barbetta’s reply, which is correct. There is one however, in case the directory has thousands of files, the runtime can be considerable, even more if we are searching in a network drive.

A faster/cleaner way to get this information would be by using a searchPattern (the same pattern we use when we search for files on Windows).

Practical example for your specific case:

string[] files = System.IO.Directory.GetFiles(@"C:\Diretorio\", "nomedoarquivo.ok*");
// Para saber a quantidade use a propriedade do array .Length
// int quantidade = files.Length;

For more information, please refer to the official documentation (which has a few more examples and explains some points if you ever want to use a *.xls filter).

Link: https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.7.2#System_IO_Directory_GetFiles_System_String_System_String_

Browser other questions tagged

You are not signed in. Login or sign up in order to post.