CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    1

    get file names in a folder / directory

    how do i get the file names of all the files in a folder / directory in c++?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: get file names in a folder / directory

    Originally posted by kalavathy
    how do i get the file names of all the files in a folder / directory in c++?
    There are no standard C++ functions to get the names of the files in a directory. You need to tell us the operating system and compiler you are using, since getting the names of the files all depend on compiler and OS.

    If you want a cross-platform solution, you can investigate the boost libraries at www.boost.org. They have a file/directory class that works on various operating systems.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    In addition to Paul's correct answer...if you are using Windows, take a look at the following FAQ. It shows how one can do it in Windows, however, it basically can be easily ported to a different operating system/compiler since the way is still the same (except for the keywords probably)...

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211
    Dear Andreas,

    Although the code in the link that u mention is quite perfect, but I saw another thread, which mentioned a minor bug in it, I mean...

    Code:
     // Check extension
              strExtension = FileInformation.cFileName;
              strExtension = strExtension.substr(strExtension.find(".") + 1);
    like, it doesn't catch files like "this.is.my.file.txt" and I guess the solution was to use reverse iterator or _splitpath(...).

    I just came through this thread and found that the FAQ is not updated, so just mentioned it, nothing personal

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Ejaz
    Although the code in the link that u mention is quite perfect, but I saw another thread, which mentioned a minor bug in it...
    Well...I would not consider this as a bug since I am still of the opinion that filenames like "this.is.my.file.txt" are invalid.

    Anyway, this can be solved by using the 'rfind()' function instead of 'find()'. Since I do not want to start a discussion about whether these names are invalid or not, I simply changed the appropriate FAQs...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured