🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #226

Re: Determining an files true type

#226
From: Remy Lebeau (TeamB)
Date: Mon, 14 Jul 2008 23:53
64 lines
3245 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message 
news:487bd893@newsgroups.borland.com...

> My code is based around the FindFirstFile / FindNextFile API's. From what 
> I can see, there doesn't seem to be a way to specify a
> wildcard search mask i.e. *.exe

Yes, there is:

    HANDLE hFile = FindFirstFile("*.exe", &fdFindFileData);

To do it recursively, however, you would need two separate searches - *.exe 
to find just files in a single folder, and then *.* to find subfolders for 
recursion.  It is easier to just use *.* by itself and then filter the 
results as needed.

> But it does not work for recursive searches.

Sure, it does:

    void __fastcall ScanForApps(const AnsiString &Folder, TStrings *Matches)
    {
        AnsiString tmpFolder = IncludeTrailingBackslash(Folder);
        WIN32_FIND_DATA fd = {0};

        HANDLE hFile = FindFirstFile( (tmpFolder + "*.*").c_str(), &fd);
        if( hFile != INVALID_HANDLE_ VALUE )
        {
            do
            {
                if( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
                    ScanForApps(tmpFolder + fd.cFileName, Matches);

                else if( AnsiSameText(ExtractFileExt(fd.cFileName), 
".exe") )
                    Matches->Add(tmpFolder + fd.cFileName);
            }
            while( FindNextFile(hFile, &fd) );
            FindClose(hFile);
        }
    }

> when I examine the properties, they appear as executable files (although 
> they appear to be back-ups and are not executable).  Based on this, I
> was wondering what the best option is to truly determine a executable?

Open each file and analyze its contents to see if a PE (Portable Executable) 
header is present.

> My first thoughts are to just ignore them as they are not executable 
> per-say, but i'm interested in how the dir commend determines there
> file type.

It looks at the file attributes, which you are not doing yet.

> Simply looking for ".exe" in the file name would not work because
> it is possible to have a text document with the name "textfile.exe.txt".

The extension on such a file is ".txt", not ".exe" or ".exe.txt".  A file's 
extension begins at the last "." in the name.


Gambit 
Technical Details
Message-ID: <487be748$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <487bd893@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,179fe31b934cb3c9
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <487bd893@newsgroups.borland.com>
Subject: Re: Determining an files true type
Date: Mon, 14 Jul 2008 16:53:05 -0700
Lines: 66
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.178.157.90
Message-ID: <487be748$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1216079688 207.178.157.90 (14 Jul 2008 16:54:48 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.isoc.lu!newsgroups.borland.com!not-for-mail