Listing of files fails for a directory which begins with a “.” ( eg: “.NET”)

I am trying to list all the files in a directory. Problem is that it keep on skipping some of the files which are under directory ".NET". Basically it skips any files under a directory name which start with a "." What might be the possible reason?

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
#include <list>
#include <fstream>

using namespace std;
void addFileToList(list<string>& fileList,const string& directory,const string& excludeFilter,const WIN32_FIND_DATA &FindFileData)
{
    string fileName = string(FindFileData.cFileName);
    string filePath = directory + fileName;

    if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) 
    {
        string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
        if (fileExtension.empty()) 
        {
            fileList.push_back(filePath);
            return;
        } 
        else if ( ( excludeFilter.find(fileExtension) == string::npos ) )
        {
            fileList.push_back(filePath);
        }
    }
}

void GetFileListing(list<string>& fileList, string directory,const string& excludeFilter,bool recursively=true)
{
    string filePath,fileName,fileExtension;
    if (recursively)
    {
        GetFileListing(fileList, directory, excludeFilter,false);
    }

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    directory += "\\";
    string filter = directory + "*";

    // Find the first file in the directory.
    hFind = FindFirstFile(filter.c_str(), &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        DWORD dwError = GetLastError();
        if (dwError!=ERROR_FILE_NOT_FOUND)
        {
            //cout << "Invalid file handle for filter "<<filter<<". Error is " << GetLastError() << endl;
        }
    }
    else
    {
        // Add the first file found to the list
        if (!recursively)
        {
            addFileToList(fileList,directory,excludeFilter,FindFileData);
        }

        // List all the other files in the directory.
        while (FindNextFile(hFind, &FindFileData) != 0)
        {
            if (!recursively)
            {
                addFileToList(fileList,directory,excludeFilter,FindFileData);
            }
            else
            {
                // If we found a directory then recurse into it
                if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && FindFileData.cFileName[0]!='.')
                {
                    GetFileListing(fileList, directory + string(FindFileData.cFileName),excludeFilter);
                }
            }
        }

        DWORD dwError = GetLastError();
        FindClose(hFind);
        if (dwError != ERROR_NO_MORE_FILES)
        {
            //cout << "FindNextFile error. Error is "<< dwError << endl;
        }
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    list<string> fileList;
    list<string>::iterator it;
    string excludeFilter;
    string directory=".";
    GetFileListing(fileList, directory,excludeFilter);

    for( it=fileList.begin(); it!=fileList.end();++it) 
    {
        cout<<(*it)<<endl;
    }

    return 0;
}

Updated the code:

Based on Hans suggestion, I added a check for the directory. Now it works! Any suggestions?

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
#include <list>
#include <fstream>

using namespace std;

void addFileToList( list<string>& fileList,const string& directory,const string& excludeFilter,const string &fileName )
{
    string filePath = directory + fileName;
    string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);

    if (fileExtension.empty()) 
    {
        fileList.push_back(filePath);
        return;
    } 
    else if ( ( excludeFilter.find(fileExtension) == string::npos ) )
    {
        fileList.push_back(filePath);
    }
}

void GetFileListing(list<string>& fileList, string directory,const string& excludeFilter,bool recursively=true)
{
    // If we are going to recurse over all the subdirectories, first of all
    // get all the files that are in this directory that match the filter

    string filePath,fileName,fileExtension;
    if (recursively)
    {
        GetFileListing(fileList, directory, excludeFilter,false);
    }

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    directory += "\\";
    string filter = directory + "*";

    // Find the first file in the directory.
    hFind = FindFirstFile(filter.c_str(), &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        DWORD dwError = GetLastError();
        if (dwError!=ERROR_FILE_NOT_FOUND)
        {
            //cout << "Invalid file handle for filter "<<filter<<". Error is " << GetLastError() << endl;
        }
    }
    else
    {
        // Add the first file found to the list
        if (!recursively)
        {
            if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
            {
                string fileName = string(FindFileData.cFileName);
                addFileToList(fileList,directory,excludeFilter,fileName);
            } 
        }

        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
        {
            // List all the other files in the directory.
            while (FindNextFile(hFind, &FindFileData) != 0)
            {
                if (!recursively)
                {
                    if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
                    {
                        string fileName = string(FindFileData.cFileName);
                        addFileToList(fileList,directory,excludeFilter,fileName);
                    } 
                    else 
                    {
                        if ( string(FindFileData.cFileName)=="." || string(FindFileData.cFileName)==".." ) continue;
                        GetFileListing(fileList, directory + string(FindFileData.cFileName),excludeFilter);
                    }
                }
            }
        }

        DWORD dwError = GetLastError();
        FindClose(hFind);
        if (dwError != ERROR_NO_MORE_FILES)
        {
            //cout << "FindNextFile error. Error is "<< dwError << endl;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    list<string> fileList;
    list<string>::iterator it;
    string excludeFilter;
    string directory=".";
    GetFileListing(fileList, directory,excludeFilter);

    for( it=fileList.begin(); it!=fileList.end();++it) 
    {
        cout<<(*it)<<endl;
    }

    return 0;
}

Answers


Try this instead:

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
#include <list>
#include <fstream>

using namespace std;

void addFileToList(list<string>& fileList, const string& directory, const string& fileName, const string& excludeFilter)
{
    string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
    if (!fileExtension.empty()) 
    {
        if (excludeFilter.find(fileExtension) != string::npos)
            return;
    } 

    fileList.push_back(directory + fileName);
}

void GetFileListing(list<string>& fileList, string directory, const string& excludeFilter, bool recursively = true)
{
    string filePath;

    directory += "\\";
    string filter = directory + "*";

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = FindFirstFile(filter.c_str(), &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        DWORD dwError = GetLastError();
        if (dwError != ERROR_FILE_NOT_FOUND)
        {
            //cout << "Invalid file handle for filter " << filter << ". Error is " << GetLastError() << endl;
        }
        return;
    }

    do
    {
        if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
        {
            if ((recursively) && (lstrcmp(FindFileData.cFileName, TEXT(".")) != 0) && (lstrcmp(FindFileData.cFileName, TEXT("..")) != 0))
                GetFileListing(fileList, directory + FindFileData.cFileName, excludeFilter);
        }
        else
            addFileToList(fileList, directory, FindFileData.cFileName, excludeFilter);
    }
    while (FindNextFile(hFind, &FindFileData) != 0);

    DWORD dwError = GetLastError();
    FindClose(hFind);

    if (dwError != ERROR_NO_MORE_FILES)
    {
        //cout << "FindNextFile error. Error is "<< dwError << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    list<string> fileList;
    GetFileListing(fileList, ".", "");

    for(list<string>::iterator it = fileList.begin(); it != fileList.end(); ++it) 
    {
        cout << (*it) << endl;
    }

    return 0;
}

Need Your Help

Finding a specific index in a binary image in linear time?

python image-processing numpy boolean python-imaging-library

I've got a 640x480 binary image (0s and 255s). There is a single white blob in the image (nearly circular) and I want to find the centroid of the blob (it's always convex). Essentially, what we're

vba/catvba class instantiation

vba catia

i want to declare class globaly

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.