Sign in to follow this  

Win32 : detecting folders/files

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, Windows, VS.NET 2008 C++ I want to detect all files/folders in a give folder.
Quote:
WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; tstring DirSpec; // directory specification int t=0; DirSpec=_T("D:/C++ Xtra/Labo 5//*"); hFind = FindFirstFile(DirSpec.c_str(), &FindFileData); if(hFind == INVALID_HANDLE_VALUE) { OutputDebugString(_T("ERROR")); OutputDebugString(_T("\n")); } else { OutputDebugString(FindFileData.cFileName); OutputDebugString(_T("\t FIRST FILE \n")); t++; while(FindNextFile(hFind, &FindFileData) != 0) { tstring temp = FindFileData.cFileName; if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) temp.append(_T("\t FOLDER")); else temp.append(_T("\t FILE")); OutputDebugString(temp); OutputDebugString(_T("\n")); t++; } } FindClose(hFind);
it works !! but it always give 2 folder to many :( '.' (==first file found) and '..' (==second found) ... what is this? there are no folders of this kind... somebody has an explanation for this ? grtz

Share this post


Link to post
Share on other sites
Those are the parent directory ".." and the other is something like that as well but I cant remember what it is.

put "dir" into command prompt and youll see what I mean. "cd .." takes you to the parent folder

Share this post


Link to post
Share on other sites
Someone has never used the command line... ;)

'.' is the current folder, '..' the parent folder. They're supposed to be there. Just sort them out when you encounter them in the enumeration.

Share this post


Link to post
Share on other sites
Boost.FileSystem is your friend.


#include "boost/filesystem.hpp"declarations
#include <iostream>
using boost::filesystem;

path my_path( "D:/C++ Xtra/Labo 5/" );

directory_iterator end_itr; // default construction yields past-the-end
for (directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
std::cout << itr->filename() <<
is_directory(itr->status()) ? " Folder" : " File" << std::endl;
}


this won't include "." or ".."

Share this post


Link to post
Share on other sites

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this