reading folder names..how is it done ?

This is a discussion on reading folder names..how is it done ? within the C++ Programming forums, part of the General Programming Boards category; hey hey, so i sat down today in my quest to increase c++ knowledge.. say i got some folders on ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    reading folder names..how is it done ?

    hey hey,

    so i sat down today in my quest to increase c++ knowledge..

    say i got some folders on my C drive ( C:/ ).

    OS is XP

    Is there a way to read the names of the folders through c++? I would love to read the names of the folders (mind u names only and not their content) and store them in a .txt file.Storing the names into the text file wont be an issue.Reading the folder names is what i am looking for.

    Really curious about this since i am practicing File I/O....

    Any help or pointers are appreciated.

    Happy Coding
    I am in love with c++...Hence i broke off with my girl

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    There's a FAQ entry...
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    thanks....i went through the FAQ (level3) Accessing a directory and all files within it...saw the linux and win32 examples....tried to study the win32 example in it for more than an hour...it all seemed overkill for what i want to do...

    i just wanna read the directories/folders on a particular drive on XP...

    perhaps i am wrong Salem...so can u please link the FAQ that u had in mind..although i am pretty sure i was in the right one..

    and since its not commented its really hard for me to understand whats going on...
    I am in love with c++...Hence i broke off with my girl

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Yes, that's the one I was thinking of.
    If you read say MSDN for the functions listed, you might find a simpler example.

    But if you ignore all the recursion (which handles sub-directories), the storage of filenames (to allow them to be sorted) then the basic "all files in a single dir" should be visible to you.

    > it all seemed overkill for what i want to do...
    You nearly always have to adapt a FAQ entry.
    It does say "level 3" though, so it's not exactly the easiest thing in the world.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like you found the right one for sure.

    It's not trivial, and the only way to find folder is to scan through ALL the items in that particular drive, and check if the "directory" attribute is there on the particular file.

    From the file-systems perspective, there's actually little difference between a normal file and a directory - except for the fact that a directory contains a list of files and directories, rather than text or some other sort of data.

    What part of the code did you think is "overkill"?

    As far as I'm aware, finding files in Windows consists of:
    FindFirstFile()
    FindNextFile()
    FindClose()

    You need to do this again and again for all directories, so that you traverse the directory tree. The simplest way to do that is to do it using a directory, but you could use a stack to track where you where last time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    Ok i will try and read some more about FindFirstFile(); FindNextFile(); and FindClose();

    I think the problem here is that i expected it to be easy...like for example i thought i would give a particular function a string parameter which is actually a path "c:\\programs " and that the function would read me the names of all the folders present at the location to which the path targets.

    it seems that if i want a function which does that i am gonna have to write it myself.Hopefully
    FindFirstFile(); FindNextFile(); FindClose() will help me in writing one...

    if anyone of you think that i am heading in the wrong direction pls lemme know...cause i am now gonna try and understand the working of the above mentioned functions and dont really wanna end up not using them after having spent 10 hrs understanding them :P
    I am in love with c++...Hence i broke off with my girl

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    You are on the right track. Initially, you would call FindFirstFile and specify the directory and the file matching pattern you want to find. It returns a handle.
    Then you call FindNextFile until it returns false, upon which time there is no more files in the current directory matching your pattern.
    FindNextFile fills a struct with information about the file or folder is found, including name, attributes, size (I think) and more.
    When you're done searching, call FindClose on the handle.

    Try to refer to MSDN for what arguments the functions take.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    Man...persistence pays in coding...and thats why i love coding.We c++ aspirants are taught..never to give up...never to surrender..for the death in a compiler is the greatest glory...err sorry wrong forums to type this in :P

    So after dissecting the win32 example in the FAQ and alt tabbing atleast a gazzilion times between msdn and the FAQs i finally understood how it works.

    I am typing the code here for the sake of other novice coders like me browsing this forum who would wanna know the solution..you may paste the code in your compiler to see how it works.Someone pls do this and confirm its working.It sure does work on my compiler VS6.0 for OS XP
    Code:
    #include <windows.h> 
    #include <iostream>
    #include <string> 
    
    using namespace std;
    
    int main()
    {
    	string Path;
    
            HANDLE hFind;
            WIN32_FIND_DATA DataStructure;         
    
    	cout << "Enter the path to the target location" <<endl;
    	cin>>Path;
            cout<<"you have entered the following path : ";
    	cout<<Path.c_str();
    
    	hFind = FindFirstFile(Path.c_str(), &DataStructure);//using .c_str member function of string class to avoid using ugly c type char arrays 
            cout<<DataStructure.cFileName<<endl;
    
            while (FindNextFile(hFind, &DataStructure))
            {
                cout << DataStructure.cFileName << endl;
            }
     
    // Close the file handle
    
        FindClose(hFind);
    
        return 0;
    }
    thats it.....just give in the path of your target directory.Remember that the first parameter to FindFirstFile(); which is the path also accepts windows wildcard charachters.Also note that i havent done any error checking..(like if the file is not found or the path is wrong blah blah blah)..because i need to have a close look at what constitutes to the structure WIN32_FIND_DATA

    Ohh and did i mention that guys on these forums are awesome....thanks to all who posted giving me pointers
    Last edited by roalme00; 01-11-2008 at 10:19 AM.
    I am in love with c++...Hence i broke off with my girl

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Well it looks OK to me, excepting the error handling you've already mentioned.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 09:18 AM
  2. Reading the names of files in a filesystem?
    By Dragoon_42 in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 06:47 AM
  3. Files not updating in folder
    By BobS0327 in forum Tech Board
    Replies: 4
    Last Post: 06-06-2005, 06:55 PM
  4. Hiding a folder
    By nextstep in forum Windows Programming
    Replies: 16
    Last Post: 03-20-2005, 03:07 PM
  5. deleting a folder AND copying a folder
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2004, 09:48 AM

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