Working with files

Categories:Common workflows

Find a file

Overview

The following example uses the Utils and File classes to recursively search for and load an .fla file.

See the XUL example for an expanded version of this script which searches for and allows you to load multiple files.

Code

function searchCallback(element)
{
    if(element instanceof File && element.extension == 'fla' && element.name.indexOf(name) != -1)
    {
        fl.openDocument(element.uri);
        return true;
    }
}

var uri = fl.browseForFolderURL('Select search folder');
var name = prompt('Enter the partial name of a file to find');

if(uri && name)
{
    Utils.walkFolder(uri, searchCallback);
}

Recursively process file content

Overview

When releasing the framework, I wanted a complete list of all the TODO items in all .jsfl and .as files in the main project repo. Doing this was fairly trivial, again using the Utils and File classes to recursively process a folder and all its subfolders, then a simple RegExp to analyse the files' content.

Code

// initialize framework
    xjsfl.init(this)
    clear();

// file / folder callback
    function callback(element, index, level, indent)
    {
        if(element instanceof File)
        {
            if(/^jsfl|as$/.test(element.extension))
            {
                // grab to do items
                    var matches    = element.contents.match(/\/\/\s*TODO([^\r\n]+)+/g);

                // process
                    if(matches)
                    {
                        trace('\n<p><strong>' +URI.toPath(element.uri, true)+ '</strong></p>');
                        trace('<ul>');
                        for each(var match in matches)
                        {
                            trace('    <li>' +match.replace(/\/\/\s*TODO\s+/, '')+ '</li>');
                        }
                        trace('</ul>');
                    }
            }
        }
    }

// recurse
    Utils.walkFolder(xjsfl.uri, callback);

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>