Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
How to check whether a file contains an extension in a folder in c#?


I am having folder with file extension FTS and FTS.SHA. I need to check whether a FTS file contains .FTS.SHA file also.
I mean if there x.FTS file then it should have x.FTS.SHA file too.

I can fetch the files as below. But how to do the above validation criteria.
C#
string[] fileList = Directory.GetFiles(COMMON.FPPath, SearchFilePattern)
                               .Select(f => f.ToUpperInvariant())
              //.Where(f => f.StartsWith("ec") || f.StartsWith("es"))
                               .ToArray();


What I have tried:

C#
string[] fileList = Directory.GetFiles(COMMON.FPPath, SearchFilePattern)
                               .Select(f => f.ToUpperInvariant())
              //.Where(f => f.StartsWith("ec") || f.StartsWith("es"))
                               .ToArray();
Posted
Updated 6-Apr-16 23:33pm

C#
if (Directory.GetFiles(dirPath, "*.*").Length == 0)
{
    //NO matching *.wma files
}
else
{
    //has matching *.wma files
}
 
Share this answer
 
Hi, Refer this code,
C#
public void CheckFiles()
{
    string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\FileCheck";

    string[] FileArray = Directory.GetFiles(FolderPath);
    foreach (string stringFile in FileArray)
    {
        FileInfo objFile = new FileInfo(stringFile);
        if (objFile.Extension == ".FTS")
        {
            string SHAFileName = objFile.Name + ".SHA";
            int FTSSHAFilesCount = FileArray.Count(FTSSHAFile => FTSSHAFile.Contains(SHAFileName));
            if (FTSSHAFilesCount > 0)
            {
                Console.WriteLine("----------------MATCH FOUND----------------");
                Console.WriteLine(objFile);
                Console.WriteLine(SHAFileName);
                Console.WriteLine("-------------------------------------------");
            }
            else
            {
                Console.WriteLine("----------------MATCH NOT FOUND----------------");
                Console.WriteLine(objFile);
            }
        }
    }
}

It is working for me, I have tested with a desktop folder FileCheck with files like, abc.FTS, abc.FTS.SHA, abd.FTS, abd.FTS.SHA, xyz.FTS.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900