Top / Programming / C++Builder / フォルダ中のファイル名とフォルダ名を取得する

フォルダ中のファイル名とフォルダ名を取得する

フォルダ中のファイル名とフォルダ名を取得するには、FindFirst()、FindNext()、FindClose()を使用します。

FindFirstの引数は「SysUtils.FindFirst」をご覧下さい。

#include <iostream>
std::locale::global(std::locale("japanese"));

TSearchRec sr;
UnicodeString path = L"C:\\WINDOWS\\*"; //フォルダのパス
int attr = faAnyFile; //ファイル属性

if (FindFirst(path, attr, sr) != 0)
{ //
  std::wcout << L"取得に失敗しました。" << std::endl;
  std::wcout << SysErrorMessage(GetLastError()).c_str() << std::endl;
}
do
{
  if ((sr.Attr & attr) == sr.Attr)
  {
    const UnicodeString name = sr.Name;
    if ((sr.Attr & Sysutils::faDirectory) != 0) //ディレクトリ
    {
      if (name == "." || name == "..") { continue; }
      std::wcout << L"フォルダ:" << name.c_str() << std::endl;
    }
    else
    {
      std::wcout << L"ファイル:" << name.c_str() << std::endl;
    }
  }
} while (FindNext(sr) == 0);
FindClose(sr);

関連

更新履歴