Include files from subfolder when importing files

Hello friends! From the series “understanding about exporting, importing files”, this time I would like to import all files from a folder with an option to include subfolder files if they exist.
Here I arrived at a functional result:

try {
const fs = require('uxp').storage.localFileSystem;
	await core.executeAsModal( async () => {
		const directory = await fs.getFolder();
		const persistentToken = await fs.createPersistentToken(directory);
		localStorage.setItem("persistentFolder", persistentToken);
		const token = localStorage.getItem("persistentFolder");
		const thePersistentFolder = await fs.getEntryForPersistentToken(token);
                const ckSubfolder = document.querySelector("#ckSubf")
		const getFiles = await thePersistentFolder.getEntries();
		for (var i=0;i<getFiles.length;i++){
			/// SCRIPTS AQUI-------[i]);
			alert("FUNCTIONS HERE!")
			/// =============
catch (e) {alert(e); console.log(e);}

But the script does not import the subfolder files, and my request is to be able to include them using a true to false checkbox, but I have no idea how to add this to my code.
Please, some wiser and more experienced friend on the subject could guide me, show me how to add the code snippet that allows such a feat.

const ckSubfolder = document.querySelector("#ckSubf")
if(ckSubfolder.checked){///CODE INCLUDE SUBFOLDER FILES}
1 Like

replace it with the following :

		const mainEntries = await thePersistentFolder.getEntries();
		for (let mainEntry of mainEntries){
            if (mainEntry.isFile){
                //it's a file in the main folder
            } else if (mainEntry.isFolder && ckSubfolder.checked){
                //it's a folder and checkbox is checked
                const subEntries = await mainEntry.getEntries()
                for (let subEntry of subEntries){
                    if (subEntry.isFile){
                        //it's a file in the sub folder
                    // and you can go deeper if you like with some refactoring and a function that handles folders

please note that I didn’t test this code and it’s a draft
basically you check the entries of the main folder and if isFolder and checkbox is checked you get entries of said subfolder and so on

1 Like

@Maher you nailed it! I didn’t even expect such a quick response!
It worked perfectly fine! Thanks for sharing.
When I grow up I want to be just like you. :handshake: :wink:

1 Like