Recursive ofxdirlist find files

Hello,
I am looking to find videos files in a set of folders.
I can not!
I have found this,


Recursive ofxDirlist …(I can not seem to upload attachments)
but I did not coder training and fails to apply the code.
Is what you would have an example?

Bernard
(Google Translate)

Precision I’m on linux
I would do everything in OF
python I get with this …

import os, fnmatch
def find_files(directory, pattern):
        for root, dirs, files in os.walk(directory):
            for basename in files:
                if fnmatch.fnmatch(basename, pattern):
                    filename = os.path.join(root, basename)
                    yield filename

    for filename in find_files('/home', '*.mov'):
        print '<sSystReturn>    ', filename

Try this! Your files will be in the files vector

void scanDir(ofDirectory dir, vector<ofFile> & files)
{
	dir.listDir();

	for(auto file : dir)
	{
		if(file.isDirectory())
		{
			scanDir(ofDirectory(file.getAbsolutePath()), files);
		}
		else
		{
			files.push_back(file);
		}
	}
}

ofDirectory dir ("...path...");
vector<ofFile> files;
scanDir(dir, files);
1 Like

Thank you Hennio
I had more or moin understand the principle but my problem is incerer this piece of code in a typical structure of OF.
void ofApp :: setup () {}
void ofApp :: update () {}

Maybe a tutorial or an example?
Sorry for my lack of knowledge.

1 Like

There are plenty of tutorials on how to use OF online.

Depending on your application you may want to put this code in different places, I’ll asume you just want it to filter for some videos

in your .h you have to add this in the ofApp class

void scanDir(ofDirectory dir, vector<ofFile> & files);

and in the .cpp

void ofApp::setup()
{
    ofDirectory dir ("...path...");
    vector<ofFile> files;
    scanDir(dir, files);

    // look for videos here!
}

void ofApp::scanDir(ofDirectory dir, vector<ofFile> & files)
{
    dir.listDir();
    for(auto file : dir)
    {
        if(file.isDirectory())
	{
		scanDir(ofDirectory(file.getAbsolutePath()), files);
	}
	else
	{
		files.push_back(file);
	}
    }
}

void ofApp::update()
{
    // empty
}

Hello
Impeccable … I could fit my project.
Thank you!
I often take the example and tutorial page but I have a bad English and do not always find my way.
The idea here is to make an application that allows me to tag the videos files with a xml file “.video.mov.xml” and allow me to manage a large number of videos
Thanks again

If you want to filter by extension an elegant way of doing so would be to use ofDirectory::allowExt() method.

In the .h

void scanDir(ofDirectory dir, vector<ofFile> & files, const vector<string> & extensions);

In the .cpp

void ofApp::setup()
{
    //...
    vector<string> extensions;
    extensions.push_back(""); // allow directory extensions
    extensions.push_back("xml"); // allow xml files
    //...
    scanDir(dir, files, extensions);
}

void ofApp::scanDir(ofDirectory dir, vector<ofFile> & files, const vector<string> & extensions)
{
    for(auto & extension : extensions)
    {
        dir.allowExt(extension);
    }
    dir.listDir();
    //...
        //...
            scanDir(ofDirectory(file.getAbsolutePath()), files, extensions);
    //...
}

Good luck!