Reading sub directories....

So i have a program that reads mp3 files from the folder i run it in... I need to be able to read mp3's from sub directories does anyone know how to do this..?

This is the code i am using


scan( "*.mp3", "C:", true );


Thanks, Jackson!
scan is not a C++ function, so you'll have to elaborate.
But in general, you just need to recursively repeat the step for every subdirectory you find.
Scan is the only way i know how to find the mp3 files in a folder... it is working for me i just dont understand how to read the subdirectories.. This is my first semester and i am finding it hard to do this? Is it just me or is it complicated? Let me know if you can help!

Thanks for the reply!
You'll have to explain where the scan function comes from or show its source code.
And no, it's not complicated. If you find a directory instead of a regular file, you'll just have to scan that directory too. You're familar with recursion, right?
He has taught the basics.. But i can post the whole code i am using if you would be kind enough to take a look?
Sure, go ahead.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <io.h> // directory functions
#include <vector>
using namespace std;

int count = 0;
void scan( string const& filespec, string startDir, bool isRecursive ) {
intptr_t hFile;
_finddata_t fd;


char last = *startDir.rbegin();
if( last != '/' && last != '\\' && last != ':' )
startDir += '/';

string findPath = startDir + filespec;
//cout << startDir << '\n';

if( (hFile = _findfirst( findPath.c_str(), &fd ) ) == -1L )
cout << "No " << filespec << " files in current directory.\n";
else {
do {
// recurse subdirectory
if( isRecursive ) {
if( (fd.attrib & _A_SUBDIR) &&
(fd.name != string(".") && fd.name != string("..") )
) {
string subDirectory = startDir + fd.name;
scan(filespec, subDirectory, isRecursive );
}
}


// output
//cout << ((fd.attrib & _A_RDONLY)?"+R":"-R");
//cout << ((fd.attrib & _A_HIDDEN)?"+H":"-H");
//cout << ((fd.attrib & _A_SYSTEM)?"+S":"-S");
//cout << ((fd.attrib & _A_ARCH)?"+A":"-A");
//cout << (fd.attrib & _A_SUBDIR);

//cout << ' ' << fd.size;
cout << ' ';
cout << "\\";
cout << fd.name;
count ++;
cout << '\n';
} while( _findnext( hFile, &fd ) == 0 );
_findclose( hFile );
cout << '\n';
}
}

int main( int argc, char* argv[]) {
string switches;
string folder;
string filespec = "*.mp3";
intptr_t hFile;
_finddata_t fd;
//char filename[100];
cout << "Please enter one of the following:" << endl;
cout << "-v for tag contents" << endl;
cout << "-h for help" << endl;
cout << "-r to process the folder recursively"<< endl;

while(cin >> switches)
{
if(switches == "-r")
{
scan( "*.mp3", "C:", true );

}else if(switches == "-h")
{
cout << "Usage: mp3report [-switches] [rootPath]" << endl;
cout << "switches = r(recursive search) | h(help) | v(verbose)" << endl;
cout << "Author: , " << endl;
return 0;
}else if(switches == "-v")
{

//scan( "*.mp3", "C:", true );

if( (hFile = _findfirst( filespec.c_str(), &fd ) ) == -1L )
cout << "No " << filespec << " files in current directory.\n";
else {
do {
cout << ' ';
cout << "File name: " <<fd.name << endl;
++count;


char tag[3];
char title[30];
char artist[30];
char album[30];
char year[4];
char comment[29];
int track[1];


//char name = fileName[0];

//cout<< "enter the mp3 filename (including the .mp3): "<< endl;
//cin.getline(filename,100);
ifstream in;//(fd.name, ios::in|ios::binary);
in.open(fd.name);
in.seekg(-128,ios::end);

if(!in)
{
cout <<"no file found" << endl;
exit(1);
}
cout << "----------------------------------" << endl;
for (int i = 0; i<3;++i)
{
char ch;
in.get(ch);
tag[i] = ch;
}

for (int j = 0; j<3;++j)
cout <<tag[j];
cout <<endl;


for (int i = 0; i<30;++i)
{
char ch;
in.get(ch);
title[i] = ch;
}

cout << "Title: ";
for (int j = 0; j<30;++j)
{
cout <<title[j];
}
cout <<endl;

for (int i = 0; i<30;++i)
{
char ch;
in.get(ch);
artist[i] = ch;
}
cout << "Aritist: ";
for (int j = 0; j<30;++j)
{
cout <<artist[j];
}
cout <<endl;

for (int i = 0; i<30;++i)
{
char ch;
in.get(ch);
album[i] = ch;

}
cout << "Album: ";
for (int j = 0; j<30;++j)
{
cout <<album[j];
}
cout <<endl;

for (int i = 0; i<4;++i)
{
char ch;
in.get(ch);
year[i] = ch;
}
cout << "Year: ";
for (int j = 0; j<4;++j)
{
cout <<year[j];
}
cout <<endl;

for (int i = 0; i<29;++i)
{
char ch;
in.get(ch);
comment[i] = ch;
}
cout << "Comment: ";
for (int j = 0; j<29;++j)
{
cout <<comment[j];
}
cout <<endl;

for (int i = 0; i<1;++i)
{
char ch;
in.get(ch);
track[i] = ch;
}
cout << "Track: ";
for (int j = 0; j<1;++j)
{
cout <<track[j];
}
cout <<endl;
cout << "----------------------------------" << endl;

in.close();
cout << '\n';
} while( _findnext( hFile, &fd ) == 0 );
_findclose( hFile );
}

}
else
{
cout << "Please enter the right form of command" << endl;
return 0;
}
cout << "total number of files: " << count << endl;
count = 0;
}
}



Thanks a lot athar you dont even understand how much i appreciate this!!
It seems that subdirectories are already processed recursively.
So what is the problem here?
It will not read from the other folders ummm let me try and explain lol

FOLDER1 - in this folder that are 3 other folders in those folders there are mp3's...

If i run the program in folder one it will read all the mp3 files.. But it will not read from the other 3 folders :(!!
If it helps i can add you on msn or something... If you can help me with some of these problems i will actually pay you on paypal i have been working on this for over 10 hours and im very po'ed that it is due in an hour and i suck lol
Well, I suppose the && last != ':' is wrong, that would result in looking for subdirectories such as "C:MP3s".
If it doesn't work for other directories either, there's probably another bug. Can't test the code without the "io.h" file.
athar i will send you the code on msn if you would like? i have two other problems if you can help me with these i will seriously pay you lmao $20 -.- i am dieing here!
I don't need payment if the problem is easy to fix (which I'm sure it is).
It probably would be easier to just post the io.h file here (or on codepad if the file is large).
Wow you are real nice lol umm sorry this might seem a little sad..... But what is the io.h file?
Well, you're including it at the top. Isn't it part of your program? Neither g++ nor mingw finds the file.
Edit: ah, wait a sec. It does come with a newer version of mingw (4.4).
Last edited on
That was an example the teacher had done and i used it in my program because i had NO idea how to do it any other way... He doesn't explain anything to us... Just throughs code on the board and expects us to know it VERY SAD... I understand java perfectly because the teacher shows us every part of the code and what it does and how to write it... Wish i had the same teacher for c++.... I am screwed! lol
Awesome my friend has some parts i don't have and i have parts he doesn't have... rewriting my code can you check back in like 20 mins? PLEASE you are like the only person that helps lol thanks so much man!
It seems that the *.mp3 mask is applied to directories too. So you should do a separate pass for finding all directories with a "*" mask.
Last edited on
Topic archived. No new replies allowed.