How to log all files inside a folder - google-apps-script

I want to navigate into a specific folder and then print all the files inside that specified folder using Google Apps Script:
function listFilesinSpecFolder(){
var files = DriveApp.getFolderById("specific folder id placed here");
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}
}
When I tried to log this it gives me an error saying that hasNext() function is not a function.
But when I run the below code it gives me no errors...
function folder(){
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
Logger.log(folder.getName());
}
}

The following script will help you 'to navigate into a specific folder and then print all the files inside that specified folder'
CODE:
function listFilesinSpecFolder() {
var folder = DriveApp.getFolderById('FOLDER ID GOES HERE');
var file;
var contents = folder.getFiles();
while(contents.hasNext()) {
file = contents.next();
Logger.log(file.getName());
}
}

var files = DriveApp.getFolderById("specific folder id placed here");
this does not return files, it will return a folder
instead try something like:
var folder = DriveApp.getFolderById("specific folder id placed here");
var files = folder.getFiles()

Related

how to save file to shourcut folder

I am trying to save attachment file from Gmail to Drive
I found a code sample here and its works BUT I need to save the file into the shared folder. at MyDrive I have a shortcut to the shared folder.
for example:
var GDRIVE_FILE = '_sheredFolder_/one/$name
function processThread(thread, label){
var attachments = message.getAttachments();
for(var i=0; i<attachments.length; i++) {
var attachment = attachments[i];
var info = {
'name': attachment.getName(),
'ext': getExtension(attachment.getName())}
var file = createFilename(GDRIVE_FILE, info);
saveAttachment(attachment, file);
}
If you have a folder shortcut in your "My Drive", then it's very likely that the folder is owned by someone else.
In order to be able to add files to a folder owned by someone else you should it should be shared with you as "editor". If it's shared with you as "viewer" you will not able to add the file to that folder.
Related
Moving Files In Google Drive Using Google Script
file.MoveTo doesn't move a file in a shared folder
Well after digging a while, I modify the core to support shortcuts or Shared folders
I add % at the begging of the initial path to identify when its a shared folder or local folder
that's the code:
var GDRIVE_FILE = '%TheSharedFolder/FOLDER/$name';
function getOrMakeFolder(path) {
var folder = DriveApp.getRootFolder();
var names = path.split('/');
while(names.length) {
var name = names.shift();
if(name.charAt(0)==='%')
{
var first = name.substring(1,name.length)
var folders = DriveApp.searchFolders('sharedWithMe');
while (folders.hasNext()) {
var folder = folders.next();
if(folder.getName()===first)
{
Logger.log("folder name is : "+folder.getName()+" folderID: " + folder.getId())
folder = DriveApp.getFolderById(folder.getId())
}
}
continue;
}
if(name === '') continue;
var folders = folder.getFoldersByName(name);
if(folders.hasNext()) {
folder = folders.next();
} else {
folder = folder.createFolder(name);
}
}
return folder;
}

Stop the function when the folder doesn't have xlsx files in (Google App scripts)

I have the next function on Google App Script:
function remove(){
var folder = DriveApp.getFolderById('folderID');
var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
while (files.hasNext()){
var file = files.next();
folder.removeFile(file);
}
}
I want stop/break/exit the function when the folder does not have/contains xlsx files.
How I do that?
Greetings
Your function should work fine. Including stopping if there are no more excel files.
But you do need to change this:
var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
(this searches the entire Drive)
to
var files = folder.getFilesByType(MimeType.MICROSOFT_EXCEL);
Here is the code:
function remove(){
var folder = DriveApp.getFolderById(''folder's id'');
var files = folder.getFilesByType(MimeType.MICROSOFT_EXCEL);;
while (files.hasNext()){
var file = files.next();
folder.removeFile(file);
}

duplicate folder and rename file structure

I'm trying to automate my duplicating folder process in google drive. As part of the process, I want to rename files and folders for each new client.
I've adapted some code found previously. It was previously working well, but for some reason now any folders/files that are more than 1 level deep of the root folder come back "undefined" in the replace section of the command.
function duplicatefolder(){
var newclientname = Browser.inputBox('Client Name')
var sourceFolder = "1. Master Client Folder";
var targetFolder = newclientname;
var source = DriveApp.getFoldersByName(sourceFolder);
var target = DriveApp.createFolder(targetFolder);
if (source.hasNext()) {
copyFolder(source.next(), target, newclientname);
}
}
function copyFolder(source, target,client) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
var newname= file.getName().toString().replace("Master",client)
file.makeCopy(newname, target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var newFolderName = subFolder.getName().replace("Master",client)
var targetFolder = target.createFolder(newFolderName);
copyFolder(subFolder, targetFolder);
}
}
The script also creates the folder in the root directory of google drive. Ideally, I'd like it to be created inside the folder "Clients". How would I add this to the script?
Appreciate the help.
Cheers
Please see attached link hope this is it what you are looking for
https://yagisanatode.com/2018/07/08/google-apps-script-how-to-create-folders-in-directories-with-driveapp/

Move all root folder files into folder without creating copies

Is there a way to move all files inside the root of Google Drive into a specified folder such as shown below without creating copies of the to be copied files?
Try this -
function myFunction() {
var files = DriveApp.getRootFolder().getFiles();
while (files.hasNext()) {
var file = files.next();
var targetFolder = DriveApp.getFolderById('FolderIDGoesHere').addFile(file);
DriveApp.getRootFolder().removeFile(file);
Logger.log(file.getName());
}
}

How to change sharing permission google drive subfolder files?

I have a main folder Folder 1
inside Folder 1 there is multiple subfolder which have files. i want to change all subfolder files sharing permission Access.ANYONE_WITH_LINK no sign in require. currently i have follow code but it only work if there is no subfolders.
function myFunction() {
var folderId = "Folder ID";
var files = DriveApp.getFolderById(folderId).getFiles();
var result = [];
while (files.hasNext()) {
var file = files.next();
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var temp = {
file_name: file.getName(),
url: "http://drive.google.com/uc?export=view&id=" + file.getId(),
};
result.push(temp);
};
}
This code will help you with your current problem:
function myFunction() {
var folderId = "main-id-folder";
// Take the mainder folder
var mainFolder = DriveApp.getFolderById(folderId);
// Take the subfolders inside your main folder
var subFolders = mainFolder.getFolders();
var result = [];
// Iterate over each subfolder
while(subFolders.hasNext()){
var subFolder = subFolders.next();
// Take the files inside the current subfolder
var files = subFolder.getFiles();
// Take each file inside the current subfolder
while(files.hasNext()){
var file = files.next();
var tmp = {
file_name: file.getName(),
url: "https://docs.google.com/spreadsheets/d/" + file.getId() + "/edit#gid=0"
};
// For the current file, stablish the permissions you want
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
result.push(tmp);
}
}
Logger.log(result);
}
The problem you had is you were only trying to get the files from your main folder but in order to get the files inside each subfolder, you needed to first take each subfolder using .getFolders(); and then for each one of them, you could use .getFiles();.
Docs
These are the docs I used to help you, in case you want to see them or anyone else who has a similar issue:
Class File
Enum Access