Ein Verzeichnis (alle Ordner und Dateien) mit findfirst und findnext auslesen

  • Im folgenden Beispiel wird der Inhalt eines Verzeichnisses (c:) in einer ListBox dargestellt.
  • Die Hilfsfunktionen ConvertTimeToStr(), ConvertDateToStr() und ConvertAttribToStr() extrahieren Fileinformationen aus dem Struct dirinfo und generieren daraus fertige AnsiStrings.
  • u.U. sollte man noch den Header dir.h einbinden
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    struct ffblk dirinfo;

    // zum 1. Directoryeintrag in c: springen und alle Filetypen suchen
    int result = findfirst("c:\\*.*", &dirinfo, FA_NORMAL | FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_DIREC | FA_ARCH);

    // solange Einträge da sind, in der Listbox ausgeben
    while (result == 0)
    {
        ListBox1->Items->Add(ConvertTimeToStr(dirinfo.ff_ftime)) +", "+ ConvertDateToStr(dirinfo.ff_fdate)) +", "+ ConvertAttribToStr(dirinfo.ff_attrib)));
        result = findnext(&dirinfo);
    }
}

AnsiString __fastcall TForm1::ConvertTimeToStr(int ffTime)
{
    return IntToStr(ffTime & 0x1F) + ":" + IntToStr(ffTime & 0x3F) + ":" + IntToStr(2 * (ffTime & 0x1F));
}

AnsiString __fastcall TForm1::ConvertDateToStr(int ffDate)
{
    return IntToStr(ffDate & 0x1F) + ":" + IntToStr(ffDate & 0xF) + ":" + IntToStr((ffDate & 0x7F) + 1980);
}

AnsiString __fastcall TForm1::ConvertAttribToStr(int ffAttrib)
{
    AnsiString sResult = "";
    if ((ffAttrib & FA_DIREC) != 0 ) sResult = sResult + "D";
    if ((ffAttrib & FA_RDONLY) != 0 ) sResult = sResult + "R";
    if ((ffAttrib & FA_HIDDEN) != 0 ) sResult = sResult + "H";
    if ((ffAttrib & FA_SYSTEM) != 0 ) sResult = sResult + "S";
    if ((ffAttrib & FA_ARCH) != 0 ) sResult = sResult + "A";
    if ((ffAttrib & FA_NORMAL) != 0 ) sResult = sResult + "N";
    return sResult;
}