Archi Forum

Archi Plug-ins => jArchi => Topic started by: ChampagnePerry on April 20, 2023, 09:52:34 AM

Title: Get all files of a given type from a directory
Post by: ChampagnePerry on April 20, 2023, 09:52:34 AM
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.
Title: Re: Get all files of a given type from a directory
Post by: Phil Beauvoir on April 20, 2023, 11:04:24 AM
Hi,

you could use some Java magic in JS like this:

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

// 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);
});

Title: Re: Get all files of a given type from a directory
Post by: ChampagnePerry on April 20, 2023, 11:08:38 AM
Thanks Phil,
I shall have a play  :)