Google Drive - a list of all files in a specified folder

Hi i am stuck here in this situation, i tried several different approaches but i cant get what i am behind it.

Situation: I have a large number of files in folders hosted in Google Drive. For example, one client reports folder has between 2000-10000 files (this will only be more), I want to be able to display these files with the ID of the name and file and parent folders so that I can create direct links to the files, In my opinion i can do it with google drive: drive-api-client-php i have a project and have client and client secret, i downloaded subversion composer and google-api and drive-api-client-php and used XAMPP

What I've done so far: I've gone through googles documentation and watch the video tutorial they offer. I tried the google sample they have on the webpage and this is the start of what I need. This will display the files in the folder with id and title or whatever fields you choose. Two questions here: you cannot specify the folder where you want the results, and the maximum number of results is limited to 1000. I enter the required 10000.

I also looked at some google apps script here that seems to be used like vba in excel to pull file ids and headers etc, however this is also limited by max results and then time issues. This is the exact type of result that I want without restriction.

My question is: What are my options when listing 10,000 caption file IDs in a specified folder on Google Drive and that this is a start-to-end process as I feel like I may be missing a fundamental point early on where my opportunities arise to get results. I am flexible about this and I can work with the data after it has been retrieved, just getting the data is key for me. looking forward to your reply, thank you very much. Hopefully this is clear enough and makes sense.

+3


source to share


2 answers


This is what I came up with thanks to the help of the above code and some other code I found, put them together and it does what I need. This might be helpful for someone else.

thank



function listFilesInFolder() {
var MAX_FILES = 2000; //use a safe value, don't be greedy
var id = 'FOLDER_ID_HERE'; 
var scriptProperties = PropertiesService.getScriptProperties();
var lastExecution = scriptProperties.getProperty('LAST_EXECUTION');
var sheet = SpreadsheetApp.getActiveSheet();
var data;
if( lastExecution === null )
lastExecution = '';

var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
  var iterator = continuationToken == null ?
  DriveApp.getFolderById(id).getFiles() : DriveApp.continueFileIterator(continuationToken);


 try { 
   for( var i = 0; i < MAX_FILES && iterator.hasNext(); ++i ) {
   var file = iterator.next();
   var dateCreated = formatDate(file.getDateCreated());
    if(dateCreated > lastExecution)
     processFile(file);
    data = [
       i,
       file.getName(),
       file.getId()
      ];        

    sheet.appendRow(data);
}
} catch(err) {
 Logger.log(err);
}

if( iterator.hasNext() ) {
 scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', iterator.getContinuationToken());
 } else { // Finished processing files so delete continuation token
 scriptProperties.deleteProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN');
 scriptProperties.setProperty('LAST_EXECUTION', formatDate(new Date()));
 }
 }

function formatDate(date) { return Utilities.formatDate(date, "GMT", "yyyy-MM-dd HH:mm:ss"); }

function processFile(file) {
var id = file.getId();
var name = file.getName();
//your processing...
Logger.log(name);

}

      

+1


source


Try this code in Script apps: you need to edit this code and replace your folder id. Open the folder on Google Drive and in the address bar of your browser you will see:

https://drive.google.com/drive/folders/Your_Folder_ID_Here

      

Copy the folder id and replace it in the code below. Google Extended Services must be explicitly enabled in two places, in Script Apps and in the Developer Console.



//This requires the Drive API To be turned on in the Advanced Google Services
function listFilesInFolder() {
  var query = 'trashed = false and ' +
      "'Your Folder ID Here' in parents";

  var filesInFolder, pageToken;

  do {
    filesInFolder = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken
    });
    if (filesInFolder.items && filesInFolder.items.length > 0) {
      for (var i = 0; i < filesInFolder.items.length; i++) {
        var thisFile = filesInFolder.items[i];
        //Logger.log('%s (ID: %s)', thisFile.title, thisFile.id);
        //To Do - Output content to file
        . . . . Code to output content . . . . 
      }
    } else {
      Logger.log('No files found.');
    }
    pageToken = filesInFolder.nextPageToken;
  } while (pageToken);
}

      

If you are looking for code written in PHP, you need to use something else.

+1


source







All Articles