Skip to main content
1-Visitor
November 1, 2012
Question

Is there a way to loop, recursively, over all the files in a directory?

  • November 1, 2012
  • 5 replies
  • 1235 views
I'd like to use Arbortext to report all the files in a directory that have a certain xpath.

It looks like xpath_boolean would tell me if a file has a particular xpath, but I didn't see a good way to loop over all the files in a directory.

Does ACL have any convenience functions for looping over all the files in a directory? If not, does anyone have any suggestions for how to go about this?

Steve


    5 replies

    18-Opal
    November 1, 2012
    Hi Steve--



    Check the archives, I'm sure I've posted some code along those lines in
    the past.



    --Clay





    Clay Helberg

    Senior Consultant

    TerraXML


    1-Visitor
    November 1, 2012
    I found a string where the subject was: acl code for many documents

    or something close to that. No xpath used, however. Clay said:

    Hi Alexander—



    Sure, you can do this. Use the glob() function to get your list of items,
    then iterate over it, something like this:



    local $files[];

    glob(“*.xml”,$files);



    # untested code, debugging is left as an exercise

    for ($file in $files) {

    local $doc = doc_open($files[$file]);

    current_doc($doc);

    sdca2b();

    doc_save($doc);

    doc_close($doc);
    }

    18-Opal
    November 2, 2012
    Thanks for digging that up, Paul.



    So, Steve, in the spot in the code where it calls "sdca2b()", that's
    where you would do your XPath test on the document and report
    success/failure, or add the filename to an array of "hits" so you can do
    something else with the list later.



    --Clay





    Clay Helberg

    Senior Consultant

    TerraXML


    1-Visitor
    November 5, 2012
    Looking at that, and another code snippet in the archives (thanks for the tip on how to find info there, Clay), here's what I came up with - I appreciate any feedback on it. I'd especially like to be able to get rid of the if statement [ if (match($filename, '\.xml')) ] but I couldn't figure out how to make the glob work with files and directories except by using \\*

    The doc for glob talks about supporting a pattern, but what kind of pattern? Is that pattern described in the doc?

    Steve
    18-Opal
    November 5, 2012
    Hi Steve--



    Just eyeballing it, it looks OK to me. One tweak that I would recommend,
    though ultimately it's a matter of preference, is I would make the XPath
    expression a parameter on the checkXpath() function instead of relying
    on a global variable for that.



    Otherwise, it looks good. I think your "if" test to identify XML files
    is an OK approach. The glob() function supports patterns as a limited
    form of regular expression, but in this case your approach is probably
    just as good. To see what subset of normal regular expression features
    is supported, search the help center for "Using Regular Expressions".



    --Clay



    Clay Helberg

    Senior Consultant

    TerraXML