Calculate the folder size with all sub-folders and files

A quick snippet from the TiDev Slack group to calculate the folder size (e.g. of the applicationDataDirectoy) without any external plug-in:

const recurseDirSize = async (dir) => {
  let file = Ti.Filesystem.getFile(dir);
  let size = 0;
  if (file.isDirectory()) {
    let subdirs = file.getDirectoryListing();
    for (let subdir of subdirs) {
      size += await recurseDirSize(dir + '/' + subdir);
    }
  } else if (file.isFile()) {
    size = file.size;
  }
  return size || 0;
};

const dir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory).resolve();
const size = await recurseDirSize(dir);

console.log("Folder size:", size);

Like the content?

If you like the tutorials, plug-ins or want to see more please consider becoming a Github sponsor.

Content