Created
September 20, 2012 11:20
-
-
Save hubgit/3755293 to your computer and use it in GitHub Desktop.
List all files in a folder (Google Apps Script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function listFilesInFolder() { | |
var folder = DocsList.getFolder("Maudesley Debates"); | |
var contents = folder.getFiles(); | |
var file; | |
var data; | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.clear(); | |
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]); | |
for (var i = 0; i < contents.length; i++) { | |
file = contents[i]; | |
if (file.getFileType() == "SPREADSHEET") { | |
continue; | |
} | |
data = [ | |
file.getName(), | |
file.getDateCreated(), | |
file.getSize(), | |
file.getUrl(), | |
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(), | |
file.getDescription(), | |
"audio/mp3" | |
]; | |
sheet.appendRow(data); | |
} | |
}; |
Looks like DocsList is deprecated by Google. You can use the below code to check the file name and ID.
/*
* Code is to check if I have a file named "ANA/20216/22.pdf" in my parent folder
*/
function findFileByNameIfPresentInAFolder(){
var folder = DriveApp.getFolderById("<Enter your parent folder ID here>");
var key = 'ANA/20216/22'+'.pdf';
var list = [];
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
if(key == file.getName()){
list.push(file.getId());
}
}
Logger.log(list);
}
I want my firebase storage and google drive synced with each other like If I upload a file to my drive it should go to firebase storage too and if I upload a file on storage it should come to my drive too.
I also want to trigger my function when I upload file on google drive or firebase storage.
Is it possible guys?
I am able to upload the folder and the file to firebase storage but its hard coded.
I want a trigger so, when drive or firebase storage changes it should trigger the function and function should take the files name either they are on drive or firebase storage and then upload it to drive or firebase either.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow this script is awesome, thank you so much for the help.
The script works great however I want to see if there is a way to add automation to it so that it either auto-updates as a new file is added to the specified google drive folder or update every X minutes? Can anyone provide assistance with this?
I thank you in advance for your time and help.
function ListNamedFilesandFolders() {
/* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293
Updated since DocsList is deprecated https://ctrlq.org/code/19854-list-files-in-google-drive-folder
*/
// List all files and sub-folders in a single folder on Google Drive
// declare the folder name
var foldername = 'GoogleDriveFolder';
// declare this sheet
var sheet = SpreadsheetApp.getActiveSheet();
// clear any existing contents
sheet.clear();
// append a header row
sheet.appendRow(["Folder","Name", "Date Last Updated", "Size", "URL", "ID", "Description", "Type"]);
// getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
// folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
var folders = DriveApp.getFoldersByName(foldername);
var foldersnext = folders.next();
// Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
// declare an array to push data into the spreadsheet
var data = [];
// list files in this folder
// myfiles is a File Iterator
var myfiles = foldersnext.getFiles();
// Logger.log("FILES IN THIS FOLDER"); DEBUG
// loop through files in this folder
while (myfiles.hasNext()) {
var myfile = myfiles.next();
var fname = myfile.getName();
var fdate = myfile.getLastUpdated();
var fsize = myfile.getSize();
var furl = myfile.getUrl();
var fid = myfile.getId();
var fdesc = myfile.getDescription();
var ftype = myfile.getMimeType();
//Logger.log("File Name is "+myfile.getName()); //Logger.log("Date is "+myfile.getLastUpdated()); //Logger.log("Size is "+myfile.getSize());
//Logger.log("URL is "+myfile.getUrl()); //Logger.log("ID is "+myfile.getId()); //Logger.log("Description is "+myfile.getDescription());
//Logger.log("File Type is "+myfile.getMimeType());
} // Completes listing of the files in the named folder
// Now get the subfolder
// subfolders is a Folder Iterator
var subfolders = foldersnext.getFolders();
//Logger.log("THE SUBFOLDER(S) ARE"); DEBUG HEADING
// now start a loop on the SubFolder list
while (subfolders.hasNext()) {
var subfolderdata = [];
var mysubfolders = subfolders.next();
var mysubfolder = mysubfolders.getName();
//Logger.log("Subfolder name:"+mysubfolder); //DEBUG
}
}