Get the Newest File in a Google Drive Folder


I have the following lines in my Google App script, but the File API is now deprecated and I am unable to update the code.

The original lines are:

var folder = DocsList.getFolder('Folder Name'); var file = folder.getFilesByType(DocsList.FileType.SPREADSHEET)[0]; var filename = file.getName(); //and get its name var fileId = file.getId(); // its ID var fileurl = file.getUrl(); //and its url var source = SpreadsheetApp.openById(fileId);`

I need my script to go to a folder on my drive, get the LATEST google spreadsheet file in this folder and open it.


Here is the code that will get the newest file in the folder:

function getNewestFileInFolder() { var arryFileDates,file,fileDate,files,folder,folders, newestDate,newestFileID,objFilesByDate; folders = DriveApp.getFoldersByName('yourFolderName'); arryFileDates = []; objFilesByDate = {}; while (folders.hasNext()) { folder = folders.next(); files = folder.getFilesByType("application/vnd.google-apps.spreadsheet"); fileDate = ""; while (files.hasNext()) { file = files.next(); Logger.log('xxxx: file data: ' + file.getLastUpdated()); Logger.log('xxxx: file name: ' + file.getName()); Logger.log('xxxx: mime type: ' + file.getMimeType()) Logger.log(" "); fileDate = file.getLastUpdated(); objFilesByDate[fileDate] = file.getId(); //Create an object of file names by file ID arryFileDates.push(file.getLastUpdated()); } arryFileDates.sort(function(a,b){return b-a}); Logger.log(arryFileDates); newestDate = arryFileDates[0]; Logger.log('Newest date is: ' + newestDate); newestFileID = objFilesByDate[newestDate]; Logger.log('newestFile: ' + newestFileID); //return newestFile; var ss = SpreadsheetApp.openById(newestFileID); Logger.log('file name that is now open: ' + ss.getName()); }; };


No worries about that, you can use DriveApp instead. Refer to this page for all the possible methods you can use on Drive files: <a href="https://developers.google.com/apps-script/reference/drive/drive-app" rel="nofollow">https://developers.google.com/apps-script/reference/drive/drive-app</a>

Hope that helps!


  • ContentFile not saved in Django model FileField
  • Ajax Form. Update Second Select when choose an option from the first one
  • upload file to S3 via cURL using presigned URL
  • OAuth With Twitter On C# .NET
  • How to execute in first a function and in second canActivate()
  • How can I get fileID or parentID in python Google-drive APIs ??
  • MPMoviePlayerController do not play movie picked from UIImagePickerController
  • How can I get the url from an ng-resource?
  • Getting Unsupported AMF version error while using AMF Connection to send flex message
  • Display original Sitecore username instead of what's currently typed in
  • Optimizing Google Sheets .appendRow()
  • Overwriting JControllerForm save() Method to Trim POST Data Has No Effect
  • File download through ajax
  • google apps script TypeError: Cannot call method “substring” of undefined
  • Playing HLS (m3u8) in Cocoa OS X AVPlayer - Swift
  • Reading and writing time values from/to a spreadsheet using GAS
  • Writing 1D array into a sheet column in Apps Script
  • Alamofire upload image with multipart/form-data
  • objective-c MPMoviePlayerViewController done button language
  • About mp3 player
  • magento sort attribute option collection by position?
  • JPA ID Generation Strategy
  • Custom comparator via explicit constructor for sorting std::set
  • How to parse nested JSON with GSON
  • Problem serializing DecimalFormatSymbols in 2.3.4
  • Issue with static code in Android
  • DriveApp conversion from DocX to PDF fails (google apps script)
  • What should be the grok pattern for thoses logs ? (ingest pipeline for filebeat)
  • How to use output buffering inside PHPUnit test?
  • How to Set wallpaper of image with its actual size?
  • Mapping ManyToMany with composite Primary key and Annotation:
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • How to request unsampled report from Google Analytics Management API using Google Apps Script?
  • Problem with rejecting incoming call
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Counter field in MS Access, how to generate?
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?