Win32API: Find all file/directory under a specific path

Today I wrote following to test win32 api. Becuase has guy ask me why can’t to list "0" directory? but I don’t know why? Due to my test program is OK.(Windows xp sp2/Visual C++ Express 2005)

first use "wcout" stdandard library object to my program. Using "cout" to character stream output under ANSI/MBSC character set and  change to use "wcout" when wide character stream ouput under wide character/Unicode.

// FindPath.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include <iostream>

// FindPath.cpp : Defines the entry point for the console application.
//

using namespace  std;

class FindPath
{
private:
    static const wchar_t m_sdir[3];
    wchar_t m_path[MAX_PATH];
    WIN32_FIND_DATA m_find_data;
    HANDLE m_handle;

public:

    FindPath(const wchar_t* path)
    {
        m_handle = INVALID_HANDLE_VALUE;
        wcscpy_s(m_path, MAX_PATH, path);
    }

    ~FindPath(void)
    {
        if (INVALID_HANDLE_VALUE != m_handle)
        {
            FindClose(m_handle);
            m_handle = INVALID_HANDLE_VALUE;
        }
    }

    const wchar_t* FindNext(void)
    {
        if (m_handle == INVALID_HANDLE_VALUE)
        {
            m_handle = FindFirstFile(m_path, &m_find_data);

            if (INVALID_HANDLE_VALUE == m_handle)
                return (wchar_t*)NULL;
        }
        else
        {
            if ( 0 ==  FindNextFile(m_handle, &m_find_data))
                return (wchar_t*)NULL;
        }

        return m_find_data.cFileName;
    }

    bool IsSelfDirectory()
    {
        if (!IsDirectory())
            return FALSE;
        return (0 == wcscmp(&m_sdir[1], m_find_data.cFileName));
    }

    bool IsUpDirectory()
    {
        if (!IsDirectory())
            return FALSE;
        return (0 == wcscmp(&m_sdir[0], m_find_data.cFileName));
    }

    bool IsDirectory(void)
    {
        return (FILE_ATTRIBUTE_DIRECTORY == (FILE_ATTRIBUTE_DIRECTORY & m_find_data.dwFileAttributes));
    }
};

const wchar_t FindPath::m_sdir[3] = {L’.’, L’.’, L"};

int _tmain(int argc, _TCHAR* argv[])
{
    FindPath foo(L"C:\*");
    locale loc(""); // same to setting "LC_ALL"

    wcout.imbue(loc);
    wcout << L"FindPath Demo 測試" << endl;
    while (const wchar_t* temp = foo.FindNext())
    {
        if (!(foo.IsSelfDirectory() || foo.IsUpDirectory()))
        {
            if (foo.IsDirectory())
            {
                wcout << L"Directory";
            }
            else
            {
                wcout << L"File";
            }
            wcout << L":’"<< temp << L"’" <<endl;
        }

    }
    wcout << L"Exit Program, press any character and enter key" << endl << flush;
    wchar_t    c;
    wcin >> c;
    return 0;
}


Update: added LC_ALL setting.

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

%d 位部落客按了讚: