• Advertisement
Sign in to follow this  

Linux + file / directory listing

This topic is 925 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

I ma trying to get all directiories and files in a folder:

void ListFiles(AnsiString dir) AnsiString is typedef std::string
{
	Directory = dir;
	s->Clear();
	d->Clear();
    DIR *pDIR;
     struct dirent *entry;
     struct stat info;
     if( pDIR = opendir(dir.c_str()) )
		{
//             while (entry = readdir(pDIR))   <-- original
         while ( (entry = readdir(pDIR)) != NULL)
             {
                     if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
                     {
                    	 stat(entry->d_name, &info);

                     if (S_ISDIR(info.st_mode)) //list directories
                    	 d->Add(entry->d_name);

                     if (Pos(mask, LowerCase(entry->d_name)) > 0 ) //list tga files only
                     s->Add(entry->d_name);

                     }
             }
             closedir(pDIR);
		}
}

Now the problem is: if (S_ISDIR(info.st_mode)) claims every file i check is directory :X

Share this post


Link to post
Share on other sites
Advertisement
To clarify bradbobak's answer:

The paths returned by opendir/readdir are relative to the directory. That is, if you opendir("foo") and there's a file named foo/bar.txt, then the file entry you get from readdir will just be "bar.txt" (no directory name). You have to reconstitute the full path yourself.

Share this post


Link to post
Share on other sites

Here the code for linux :

void ScanFolder( const CString& Path, const bool Recursive, CArray< CString >* Output )
{
  // Open the dir.
  DIR* OpenedDir = opendir( Path.GetData() );

  // Check if we can't open the dir.
  if( OpenedDir == nullptr )
    return;

  // Reset the position of the directory stream.
  rewinddir( OpenedDir );

  // First result.
  dirent* Result = readdir( OpenedDir );

  // Add each file found.
  while( Result != nullptr )
  {
    // Recursive if we found a folder.
    if( ( Result->d_type == DT_DIR ) && Recursive )
    {
      if( ( strcmp( Result->d_name, "." ) != 0 ) && ( strcmp( Result->d_name, ".." ) != 0 ) )
        ScanFolder( Path + Result->d_name + "/", Recursive, Output );
    }
    else
    {
      Output->Add( Path + Result->d_name );
    }

    // Next result.
    Result = readdir( OpenedDir );
  }

  // Close the dir.
  closedir( OpenedDir );
}

One "if" can be added to be sure a slash '/' is at the end of the path.

 

How to use it :

ScanFolder( "Folder/", true, &OutputArray );
Edited by Alundra

Share this post


Link to post
Share on other sites

 

void ScanFolder( const CString& Path, const bool Recursive, CArray< CString >* Output )

You name variables with a capital letter? You are mad. Mad, I tell you. tongue.png

 

Why?

I use capitol's for parameters and lowercase for local variables and a "PV_" prefix instead of the "m_"(only found out about that after using PV_).

Share this post


Link to post
Share on other sites

The good practice is to respect the convention you use from the start to have a consistent API.

Edited by Alundra

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement