Is there a script call to check number of files in a folder?

Hi everyone 🙂

Thanks in advance for any help.

I'm wondering if there's a script or script call I can use to check the number of files inside a folder?... As in return into a variable the number of files in /img/pictures for example...
This'll give you the amount of images loaded in the image cache so you could filter the result by the folder path
Object.keys(ImageManager._imageCache._items).length
This'll give you the amount of images loaded in the image cache
I'm not sure I understand the intent here. Not all of the files in the pictures folder are going to be loaded into the cache at once. In fact, odds are very few (or none) of them will be at any given time.

Also, the ImageManager in MZ doesn't have an _imageCache property.

I'm wondering if there's a script or script call I can use to check the number of files inside a folder?
I Googled "javascript number files directory" and got this:

You would simply type your desired directory where it says "directory" and then replace the console.log() with whatever you want to do - presumably put it into a variable? So this:
1733894433844.png


becomes
Code:
const fs = require("fs");
const dir = "./img/pictures";
fs.readdir(dir, (err, files) => $gameVariables.setValue(2, files.length));

Replacing the 2 with the ID of the variable you want to store the number in.
Not all of the files in the pictures folder are going to be loaded into the cache at once. In fact, odds are very few (or none) of them will be at any given time.
My bad, I was thinking they were so I thought to filter and count the cache.
My bad, I was thinking they were so I thought to filter and count the cache.
Gotcha - the cache is what images are being kept in memory. So that's whatever is required for the current map to display (tilesets and events) and anything that's permanently kept in the system cache.

Then whatever additional things get loaded by events - pictures, animations, etc.
Thanks ATT_Turan! That's exactly what I need, tested and it works, however I have another question.

In your example you have this - const dir = "./img/pictures";
Which in my case is this - const dir = "./img/pictures/Alaska/Beach";

If I enter that it works just fine, however I want the word "Alaska" to come from a variable e.g. (Mexico, US, France)

I tried - const dir = "./img/pictures/" + $gameVariables.value (1) + "/Beach";
That didn't work.
This didn't work either -
gameVariables.setValue(8, $gameVariables.value(6) + $gameVariables.value(21) + $gameVariables.value(7));
- It returns ./img/pictures/Alaska/Beach without the quotes of course, and if I use '"./img/pictures/' to include the double quotes it just crashes.

Any advice?
Thanks in advance
You can use JavaScript template literals with back ticks:
Code:
const dir = `./img/pictures/${$gameVariables.value(1)}/Beach`;

Should do the trick.
I'll try that and let you know.
Thanks 🙂

Yeah! That worked awesome!!! Everything is prefect now!
Thank you so much!
Please note that this will not work if you deploy to web browsers. Just something to keep in mind.

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-script-call-to-check-number-of-files-in-a-folder.173821/