How to recognize file extension?

Is there any code to recognize, that a file has certain extension? For example this code would search through a folder and any file in it with *.exe extension would be counted or somewhat else.
JackKutiAsked:
Who is Participating?
 
atul_parmarConnect With a Mentor Commented:
1. If you want to get an extention from a filename use ExtractFileExt funtion.
2. If you want to search for specific pattern (as ciuly said) use the following code.

var
  sr: TSearchRec;
  path : string;
begin
  path := 'c:\*.exe';
  if FindFirst(Path, faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory) <> faDirectory then
      begin
        Memo1.Lines.Add(sr.Name)
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end
end;
0
 
2266180Commented:
I don't exactly understand the problem, but yes, findfirst/findnext with *.exe will get all files/directories that have an extension of "exe" (you can eliminate directories by putin a -faDirectory in the flags of findfirst.
also, extractfileext from sysutils get's you the extension in case you already have some filename.
0
 
2266180Commented:
hm...
since the only difference between atul_parmar's solution and mine was that he also gave some source code, the correct way to go would have been a point-split. I am not saying that you should modify the status of this question (leave it, not a big deal), just to be more "carefull" in the future. I had cases when spent quite some time on getting good source and someone just copied my source and made an "imrpovement" (in the user interface) and he got the points. such cases are frustrating. this one was a few seconds ":job" so there is no problem from my part, but as I said: in future cases pay more attention at all solutions and try to give a fair outcome to the issue.

peace :)
0
 
atul_parmarCommented:
Coool man. its all about presentation.

I never fix an issue for points. If it's money I ofcourse.

Debgging and fixing is my passion.


0
 
JackKutiAuthor Commented:
I´ll be more careful next time...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.