Linux + file / directory listing

Started by
6 comments, last by Alundra 8 years, 6 months ago

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

Advertisement

Maybe stat is failing? (You may need to append the filename to the directory to pass to stat).

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.

Sean Middleditch – Game Systems Engineer – Join my team!

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 );

i was unable to get out of while loop using that code

however when i used if ( entry->d_type == DT_DIR ) in my code evenything now works as expected. thanks alot

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

Sean Middleditch – Game Systems Engineer – Join my team!


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_).

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

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

This topic is closed to new replies.

Advertisement