When you want to find and copy files of specific types using Google Apps Script from a folder to another folder on Google Drive, this code for sure will come handy. As you know Google Apps Script has a functionality of running it automatically on a particular time (time driven trigger) or even driven trigger(like opening of a spreadsheet), this makes it the best possible services offered by google and specially when you want to interact between different services offered by Google (as shown in below image).
Find and Copy files of specific types in a folder on Google Drive using Google Apps Script – Code :
//Code to list files in a folder and Copy CSV and Excel Files from a Source Folder to Destination Folder function GetFilesInAFolder() { var SourceFolder = DriveApp.getFolderById("SourceFolderID") var DestinationFolder = DriveApp.getFolderById("DestinationFolderID") var Files = SourceFolder.getFiles(); var CSVFiles = SourceFolder.getFilesByType(MimeType.CSV) var ExcelFiles = SourceFolder.getFilesByType(MimeType.MICROSOFT_EXCEL) while (CSVFiles.hasNext()) { CSVFiles.next().makeCopy(DestinationFolder) } while (ExcelFiles.hasNext()){ ExcelFiles.next().makeCopy(DestinationFolder) }
Basically in the above code what i did is, first created two variable having Source and Destination folder and then find the specific file types i.e. CSV and Microsoft Excel using the MimeType.CSV and MimeType.MICROSOFT_EXCEL respectively. And finally copied both files types to destination folder.
To get more detailed knowledge about Google Apps Script, you can check the official website. Also if you want to explore more codes/posts related to Google Apps Script on our website, then you can find it here.
If you have any questions or if you need any help please get in touch with me using the comment section below.
Leave a Reply