How to get list of all files in Google Drive

How to get list of all files in Google Drive

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.