Google Apps Script ➡️ Utils. Get files from the 📂folder

Max Makhrov
2 min readAug 9, 2023

The code here is the part of “Utils” project on GitHub.

The code:

/**
* @typedef {Object} FileInfo
* @property {String} id
* @property {String} name
* @property {String} mime_type
* @property {Integer} size
*
*/

/**
* @param {String} folderId
*
* @returns {Array<FileInfo>} files
*/
function getFolderFiles_(folderId) {
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
var file, result = [];
/** @type FileInfo*/
var info;
while (files.hasNext()) {
file = files.next();
info = {
id: file.getId(),
name: file.getName(),
mime_type: file.getMimeType(),
size: file.getSize()
}
result.push(info);
}
return result;
}

Test:

function test_getFolderFiles() {
var folderId = "1cGHfhOJzI4QUeY47ktYy6sRdMA0ODYOX";
var res = getFolderFiles_(folderId);
console.log(res);
}

Results:

[ { id: '1ioyLMh-yzNrk8HVo6T1FG_GYqtQaE6qkTkdny1r9mSE',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1WU1ICn-o0BloQ8uOL7RvSTGNIiSTFJGEZKjvjzRva9M',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1U2NYukVwTli-apClSu2cNPINxPNAsFwRQoCaiMLbYHY',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1bYj-_6bv-spvqg3UjXLrOV-jvwTJ_F4SyleQKwLgURU',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1f3IIHt3S75x2U5Mph-8vOHd3JLXVdrYD0UftYdbxEgw',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1nZlX5WB3dPV-Wj_-SvoHkJqNZK--KAp-5D1aH5r0x8g',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1C-S39DnQOo6YY5Wy7H7KbiFcbFCcNrIpCmfoxQkEsOQ',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1c9xPESVYBQwL76pv4BSBfm-kAZiOo1yviyC9-5kbnJE',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1THP0S6-bXu2EIK-HWY7MT-8KJTyYH7Yy5d3W0mSbCBU',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1IB8FzczQxdZAQDViTE6RzTVSUt2gDnFCybH67dDAQOQ',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1ou_5zX0gfFBkrVJDs_ITJGttWiRUUm8JWSFn0uelvo0',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1fRDamMANmAbUF1FX3jU6-VWren9kbjZxzdH4lD_-4JQ',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1FVDQm-Thjhak_mPs-l3BSrZ7-Zj-3FCmZjCefR72iGM',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36308 },
{ id: '1hBBSMoJDL5vyIRd3cDpgHKn_7UTkvb_mzWFaJ91QlLY',
name: 'Dummy Data. Generated by Formulas',
mime_type: 'application/vnd.google-apps.spreadsheet',
size: 36028 } ]
Photo by Garmin B on Unsplash

--

--

Max Makhrov

Google Sheets Developer, master of Online Accounting