checking whether a folder is empty

This is a discussion on checking whether a folder is empty within the C++ Programming forums, part of the General Programming Boards category; Hello. Could anyone please give me a pointer to where I could find out more about how to do a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26

    checking whether a folder is empty

    Hello.
    Could anyone please give me a pointer to where I could find out more about how to do a check in the program whether a folder is empty or not? I dont really know what to search for, so just knowing that would been very usefull for me.

    can it be done with fstream in some ways, or is there any own functions to do this?

    Dag

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you have windows, call FindFirstFile in the folder. If it returns no result it should be empty. It will find subfolders but these can be checked through flags.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    thank you , what about linux?

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    Ok I did a try now, but somehow it does not work correctly-- it allways goes to the else clause, and there it allways just couts a dot(.), not any files, for example when I input "C:\\WINDOWS\\*".
    Can anyone please correct me?

    Code:
    // Finding first file in a folder test
    
    #include <iostream>
    #include <windows.h>
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main() {
    
        char folder[100];
        cout << "Enter folder: ";
        cin >> folder;
        
        cout << "Checking " << folder << endl;
            
        WIN32_FIND_DATA found;
        HANDLE hFind;
        hFind = FindFirstFile(folder, &found);
        
        if (found.cFileName == NULL) { // its probably this line, but...... Why?
            cout << "Folder does not exist or folder is empty." << endl; 
        }
        else {    
            cout << found.cFileName << " is the first file in " << folder << endl;
        }    
        
        system("pause");
        
        return 0;   
    }
    Last edited by Dag_; 03-07-2005 at 05:41 AM. Reason: I just noticed I had not tested with C:\\WINDOWS\\* which I am suppose to, but still it only returns a dot no matter what folder it is

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    WIN32_FIND_DATA.cFileName is an array so it will never ever whatsoever be NULL.

    Check the return value from FindFirstFile instead. If it's INVALID_HANDLE_VALUE no file was found.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    thanks
    life is too short smart but hard !

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Checking if buffer is completely empty...
    By cookie in forum C Programming
    Replies: 8
    Last Post: 07-13-2008, 03:45 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

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