[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Acquiring all files in a folder
From: |
Garrett Kajmowicz |
Subject: |
Re: Acquiring all files in a folder |
Date: |
Wed, 01 Dec 2004 19:55:08 -0500 |
User-agent: |
Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) |
On Wed, 01 Dec 2004 16:24:50 -0800, jakevoytko wrote:
> I am working with a group to write a program that organizes mp3 files
> in a root folder based on tag criteria. The tag readers are written,
> we are having trouble finding g++ documentation on reading all files
> in a folder, and deciding which files are mp3's. Any help will be
> appreciated.
What you want to do is not specific to g++, but is an operating-system
specific feature. Here is some code which does (sort of) what you want.
I hacked this apart from a library loading routine I wrote, so it may need
a little tweaking and look a little ugly.
//Needed for Unix library routines - cross platform how?
// Conforms to POSIX 1003.1-2003
#include <dlfcn.h>
#include <sys/types.h>
#include <dirent.h>
std::vector<std::string> openModules(const std::string path){
std::vector<std::string> retval;
//Open up the required directory to search for modules
printf("Loading libraries from: %s\n", path.c_str() );
DIR * d = opendir(path.c_str());
if(d == NULL){
return retval;
}
struct dirent * f;
//Get the name of each file
do{
f = readdir(d);
if(f != 0 ){
int stringlength = strlen(f->d_name);
//Check to make sure that the file extansion is ".mp3"
if( strncmp(f->d_name + stringlength - 4, ".mp3", 4) == 0 ){
std::string filename = path + "/";
filename += f->d_name;
//Check to make sure that the file actually contains mp3 data - how?
if( test_function(filename.c_str()) ){
retval.push_back(filename);
}
}
}
}while(f != 0);
return retval;
}
The code listed will return a listing of all files in a directory which
are mp3 files.
- Garrett Kajmowicz