can anyone help me in reading the file names from a folder with a specific extention in VC++ enviroment.

thanks in advance
sachin

Dani AI

Generated

was right to ask for posted code; having what failed narrows down causes. 's MFC suggestion (CFileFind) is a valid solution when building an MFC app. For modern Visual C++ projects a clearer, portable option is the C++17 <filesystem> library; for legacy non-MFC Win32 code the FindFirstFile/FindNextFile API remains appropriate.

A concise std::filesystem example (C++17+) that lists files with a given extension:

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    fs::path dir = "C:\\path\\to\\folder";
    fs::path ext = ".txt"; // include the dot
    if (!fs::exists(dir) || !fs::is_directory(dir)) return 1;
    for (const auto& e : fs::directory_iterator(dir))
        if (e.is_regular_file() && e.path().extension() == ext)
            std::cout << e.path().filename().string() << '\n';
}

Notes and troubleshooting: compile with C++17 enabled (MSVC: /std:c++17). On some older libstdc++ versions linking may require -lstdc++fs. path().extension() returns the dot-prefixed extension and is case-sensitive; normalize case on Windows if necessary. Wrap filesystem calls in try/catch for std::filesystem::filesystem_error. For very long or Unicode paths on older Windows use wide Win32 APIs or MFC as shown by . Further reference: the C++ filesystem overview (cppreference filesystem) and MFC CFileFind docs (CFileFind class).

Recommended Answers

All 2 Replies

Have you written anything yourself on this?

We usually help you troubleshoot your code, not write it for you.

Heres a snippet in mfc for finding a file of form *.extension in directory = myDirectory

// Get a list of files
   CFileFind finder;
   BOOL finding = finder.FindFile (myDirectory + CString ("\\*.") + extension);
   
   //iterate the results
   while (finding)
    {
      finding = finder.FindNextFile();
    
      //get file path
      CString path = finder.GetFilePath());
     
      //get file title
      CString title = finder.GetFileTitle() 
    }

    //dont forget to close
    finder.Close ();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.