I have the below script - which finds all the files in a folder (including files in subfolders) and deletes them, however, I want this script to delete all the folders in the parent folder as well. So basically the entire contents of the parent folder are wiped.
function DeleteInvoices() {
const getAllFiles = folder => {
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
console.log(file.getName());
file.setTrashed(true);
}
const folders = folder.getFolders();
while (folders.hasNext()) getAllFiles(folders.next());
}
var folder = DriveApp.getFolderById("1-2VAPBzEQxCaoN7KGSB_K_peiLIDk32e");
getAllFiles(folder);
}
Any help would be greatly appreciated :)
To trash the files and folders contained in a parent folder, you only need to iterate through the first level
Nested files and folders will be automatically trashed if their parent folder is trashed
The method setTrashed(trashed) is not only available for files, but also for folders
Working sample:
function DeleteAllContents() {
var folder = DriveApp.getFolderById("1-2VAPBzEQxCaoN7KGSB_K_peiLIDk32e");
const files = folder.getFiles();
while (files.hasNext()){
var file = files.next();
file.setTrashed(true);
}
const folders = folder.getFolders();
while (folders.hasNext()){
var folder = folders.next();
folder.setTrashed(true);
}
}
Related
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;
}
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()
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/
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());
}
}
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