How to recognize file extension?

Posted on 2006-06-02
Last Modified: 2010-04-05
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.
Question by:JackKuti
    LVL 28

    Expert Comment

    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.
    LVL 10

    Accepted Solution

    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.

      sr: TSearchRec;
      path : string;
      path := 'c:\*.exe';
      if FindFirst(Path, faAnyFile, sr) = 0 then
          if (sr.Attr and faDirectory) <> faDirectory then
        until FindNext(sr) <> 0;
    LVL 28

    Expert Comment

    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 :)
    LVL 10

    Expert Comment

    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.


    Author Comment

    I´ll be more careful next time...

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now