-
-
Save hubgit/3755293 to your computer and use it in GitHub Desktop.
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); | |
} | |
}; |
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());
// Populate the array for this file
data = [
foldersnext,
fname,
fdate,
fsize,
furl,
fid,
fdesc,
ftype
];
//Logger.log("data = "+data); //DEBUG
sheet.appendRow(data);
} // 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
// Get the files
// mysubfiles is a File Iterator
var mysubfiles = mysubfolders.getFiles();
//Logger.log("FILES IN THIS FOLDER"); //DEBUG HEADING
// now start a loop on the files in the subfolder
while (mysubfiles.hasNext()) {
var smyfile = mysubfiles.next();
var sfname = smyfile.getName();
var sfdate = smyfile.getLastUpdated();
var sfsize = smyfile.getSize();
var sfurl = smyfile.getUrl();
var sfid = smyfile.getId();
var sfdesc = smyfile.getDescription();
var sftype = smyfile.getMimeType();
//Logger.log("Subfolder is "+foldersnext+"/"+mysubfolder);
//Logger.log("File Name is "+ smyfile.getName()); Logger.log("Date is "+ smyfile.getLastUpdated()); Logger.log("Size is "+ smyfile.getSize());
//Logger.log("URL is "+ smyfile.getUrl()); Logger.log("ID is "+ smyfile.getId()); Logger.log("Description is "+ smyfile.getDescription());Logger.log("File Type is "+ smyfile.getMimeType());
subfolderdata = [
(foldersnext+"/"+mysubfolder),
sfname,
sfdate,
sfsize,
sfurl,
sfid,
sfdesc,
sftype
];
//Logger.log("subfolderdata = "+subfolderdata);
sheet.appendRow(subfolderdata);
}
}
}
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.
For anyone having problems with the timeout issue - Check this here; https://stackoverflow.com/questions/30428074/google-drive-how-to-list-all-files-in-a-specified-folder