Get all files of a given type from a directory

Started by ChampagnePerry, April 20, 2023, 09:52:34 AM

Previous topic - Next topic


OK I know this is a basic question but I'm not really a javascript developer so you'll have to forgive me.
Does anyone have an example of how to get a list of files from a directory.
I know there is the window.promptOpenDirectory() that I can use to get a directory path but how do I then get a list of the files in the directory I select?
I have a directory with a load of SQL DDL files in and I want to iterate through them so I can parse them to automatically populate a model with elements.
I have written a script to parse and populate a model with the basic table info from a single file using promptOpenFile but I want to do all the files in the directory as each tables definition has been saved as a separate file.
Thanks for any help.
I will post the resulting script on the gist after I finish it if anyone wants a laugh.

Phil Beauvoir


you could use some Java magic in JS like this:

// Java File class
const File = Java.type("");

// Ask user for folder
const folder = window.promptOpenDirectory();

// Get file names in folder
const files = new File(folder).list();

// Each file...
files.forEach(f => {
    console.log(folder + f);

If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.