open/search a folder when searching a directory

This is a discussion on open/search a folder when searching a directory within the C++ Programming forums, part of the General Programming Boards category; Code: void dir() { HANDLE hFind; WIN32_FIND_DATA FindData; int ErrorCode; BOOL Continue = TRUE; cout << "*.exe Files in WINDOWS ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Question open/search a folder when searching a directory

    Code:
    void dir()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        int ErrorCode;
        BOOL Continue = TRUE;
    
        cout << "*.exe Files in WINDOWS directory." << endl << endl;
    
        hFind = FindFirstFile("C:\\Windows\\*.exe", &FindData);
    
        if(hFind == INVALID_HANDLE_VALUE)
        {
            ErrorCode = GetLastError();
            if (ErrorCode == ERROR_FILE_NOT_FOUND)
            {
                cout << "There are no files matching that path/mask\n" << endl;
            }
            else
            {
                cout << "FindFirstFile() returned error code " << ErrorCode << endl;
            }
            Continue = FALSE;
        }
        else
        {
            cout << FindData.cFileName << endl;
        }
    
        if (Continue)
        {
            while (FindNextFile(hFind, &FindData))
            {
                cout << FindData.cFileName << endl;
            }
    
            ErrorCode = GetLastError();
    
            if (ErrorCode == ERROR_NO_MORE_FILES)
            {
                cout << endl << "All files logged." << endl;
            }
            else
            {
                cout << "FindNextFile() returned error code " << ErrorCode << endl;
            }
    
            if (!FindClose(hFind))
            {
                ErrorCode = GetLastError();
                cout << "FindClose() returned error code " << ErrorCode << endl;
            }
        }
        cout << endl;
    }
    I used this script from a website.

    How am I able to open and search a folder every time the program encounters one in the windows folder?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    A common method is to call your method recursively if the file found has the directory flag attribute on. You would pass your method a path to the directory to enumerate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 10:52 PM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Not able to access directory of home folder
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-06-2008, 02:45 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21