How to get list of all files in Google Drive

How to get list of all files in Google Drive

Small file management tasks can be performed by Google Drive alone. But when it comes to a large number of files, then it can become a hassle for Google Drive to manage the task alone. With Google Apps Script, you can get the list of all files or a large number of files executed in less than 6 minutes. Thus, aiding businesses in the efficient management of time, files, and finances. 

In this article, we will be helping you in improving your large batches of file management skills. You can use the below-mentioned Google Script code to get a list of all files in Google Drive as well as their metadata. 

The code will fetch the list of all files in the Google Drive’s root folder, file URL, file ID, file creation date, file modification date, file size, ownership information, and mime type. You can also use the code below for adding any necessary relevant data in the most hassle-free way.

To get a list of all files in Google Drive along with their metadata using Google Apps Script, you can use the following code:

Copy code// Get a reference to the root folder of Google Drive
var rootFolder = DriveApp.getRootFolder();

// Get a list of all files in the root folder
var files = rootFolder.getFiles();

// Create an array to store the file metadata
var fileMetadata = [];

// Loop through the files and get their metadata
while (files.hasNext()) {
  var file = files.next();
  fileMetadata.push({
    name: file.getName(),
    id: file.getId(),
    url: file.getUrl(),
    createdTime: file.getDateCreated(),
    modifiedTime: file.getLastUpdated(),
    size: file.getSize(),
    mimeType: file.getMimeType(),
    owner: file.getOwner().getEmail()
  });
}

// Write the file metadata to a sheet in a Google Sheets document
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.clear();
sheet.appendRow(['Name', 'ID', 'URL', 'Created Time', 'Modified Time', 'Size', 'MIME Type', 'Owner']);
for (var i = 0; i < fileMetadata.length; i++) {
  sheet.appendRow([    fileMetadata[i].name,
    fileMetadata[i].id,
    fileMetadata[i].url,
    fileMetadata[i].createdTime,
    fileMetadata[i].modifiedTime,
    fileMetadata[i].size,
    fileMetadata[i].mimeType,
    fileMetadata[i].owner
  ]);
}

This code will get a list of all files in the root folder of Google Drive, retrieve their metadata, and then write the metadata to a sheet in a Google Sheets document.

The metadata includes the file name, file ID, URL of the file, date when the file was created, date when the file was modified, size of the file, mimetype and ownership information. You can add other relevant metadata if necessary.