Thread: How to display all files, subfiles and directories

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    3

    Question How to display all files, subfiles and directories

    I know about FindFirstFile/FindNextFile, so how can i display along the path, for example, C:\Program Files\, all folders and all files enclosed within them, in general, all directories and files along this path. Using only WinAPI + FindFirstFile/FindNextFile, without Boost/Filesystem/dirent.h and etc. Looks like a recursion output with dirent.h, such as:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <dirent.h>
    #include "iostream"
    #include "string"
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    void listFilesRecursively(char *path);
    int main()
    {
      setlocale(LC_ALL , "Russian");
        char path[100];
        printf("Enter path to list files: ");
        scanf("%s", path);
        listFilesRecursively(path);
        return 0;
    }
    void listFilesRecursively(char *basePath)
    {
         WIN32_FILE_ATTRIBUTE_DATA Info;
         char path[1000];
         struct dirent *dp;
         DIR *dir = opendir(basePath);
        if (!dir)
            return;
        while ((dp = readdir(dir)) != NULL)
        {
            if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
            {
                printf("%s\n", dp->d_name);
    
                strcpy(path, basePath);
                strcat(path, "/");
                strcat(path, dp->d_name);
    
                string path_s = (const char*) path;
                GetFileAttributesEx(path_s.c_str(), GetFileExInfoStandard, &Info);
                cout<<Info.nFileSizeLow<<endl;
                listFilesRecursively(path);
            }
        }
    
        closedir(dir);
    }

    But how to do this using only WinAPI?

    I tried like this:
    Code:
    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <vector>    
    
    void FindFile(const std::wstring &directory)
    {
        std::wstring tmp = directory + L"\\*";
        WIN32_FIND_DATAW file;
        HANDLE search_handle = FindFirstFileW(tmp.c_str(), &file);
        if (search_handle != INVALID_HANDLE_VALUE)
        {
            std::vector<std::wstring> directories;
    
            do
            {
                if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if ((!lstrcmpW(file.cFileName, L".")) || (!lstrcmpW(file.cFileName, L"..")))
                        continue;
                }
    
                tmp = directory + L"\\" + std::wstring(file.cFileName);
                std::wcout << tmp << std::endl;
    
                if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                    directories.push_back(tmp);
            }
            while (FindNextFileW(search_handle, &file));
    
            FindClose(search_handle);
    
            for(std::vector<std::wstring>::iterator iter = directories.begin(), end = directories.end(); iter != end; ++iter)
                FindFile(*iter);
        }
    }
    
    int main()
    {
        FindFile(L"C:");
        return 0;
    }

    But it only displays if the path is in the form of "C:" and does not display subfolders with files.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    692
    Maybe more like this. (Sorry, but I couldn't test it).
    Code:
    void FindFile(std::wstring dir)
    {
        dir += L"\\*";
     
        WIN32_FIND_DATAW file;
        HANDLE search = FindFirstFileW(dir.c_str(), &file);
        if (search == INVALID_HANDLE_VALUE) return;
     
        dir.pop_back(); // remove asterisk
     
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                (!lstrcmpW(file.cFileName, L".") || !lstrcmpW(file.cFileName, L"..")))
                continue;
     
            auto path = dir + file.cFileName;
            std::wcout << path << std::endl;
     
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                FindFile(path);
        }
        while (FindNextFileW(search, &file));
     
        FindClose(search);
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    !!!UPDATE!!!


    Sir, it started without errors on MS VS 19.
    But again display not all files and does not work with path C:\windows and so on, when C:\ + any path.
    Could you help me make the user input the path? For example C:\Program Files\. With spaces too.
    Last edited by Graduate2019; 4 Weeks Ago at 12:57 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,525
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,232
    Quote Originally Posted by Graduate2019 View Post
    it started without errors on MS VS 19.
    Since you're using such a recent IDE with a compiler that supports C++17, have you considered using the standard filesystem library instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,455
    That was already suggested in the link pointed out by Salem.

    By the way this topic should really be in the Windows forum, since using the Windows api is a requirement.

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    Welcome all!
    Salem, it's my topic on other site too (why you left a link to this forum in that thread?).
    Amanda, i can not using the standard filesystem library instead... it is necessary and spelled out in my assignment. I tried to do using dirent.h and i made it, but the teacher said to do only using WinAPI with FindFirstFile/FindNextFile.
    I understand that I need to deeply understand WinAPI, but I have a completely different profile of work, this is not c++ at all, sorry.
    offtop You have a nice site, but why there is not automatic date in footer? /offtop
    Last edited by Graduate2019; 4 Weeks Ago at 12:47 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,525
    > Salem, it's my topic on other site too (why you left a link to this forum in that thread?).
    It's for the benefit of everyone who might care enough to help you.
    Like for example,
    - not wasting time saying what's already been said elsewhere
    - finding out critical information you've omitted (like say must use WinAPI)

    And of course - the FAQ
    Accessing a directory and all the files within it - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directories as regular files
    By Aculaniveus in forum C Programming
    Replies: 3
    Last Post: 01-26-2010, 04:26 PM
  2. S_ISDIR returns directories and files?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2009, 01:56 PM
  3. Directories and files using dirent.h
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 05:10 PM
  4. How to add C++ files from directories in makefile.am
    By Bargi in forum Linux Programming
    Replies: 0
    Last Post: 10-15-2007, 04:43 AM
  5. Accessing files/directories with different permission
    By Opel_Corsa in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2007, 05:37 PM

Tags for this Thread