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/
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()
So I created my first script by copying and pasting stuff from others. Somewhere down the line it look like I removed something that allows for triggers (both time & onchange) to work.
I tried applying some suggestions from other questions like these but without result..
function DolistFilesInFolder() {
listFilesInFolder("specified folder"); //Enter folder name in between " "
}
function listFilesInFolder(folderName) {
Logger.log(folderName);
var folders = DriveApp.getFoldersByName(folderName);
var folder = folders.next();
var contentfolders = folder.getFolders(); //fetches the folders inside the folder
var contentfiles = folder.getFiles(); //fetches the files inside the folder
// Find or add sheet with folder name
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(folderName);
if (sheet) {
Logger.log("found");
}
else {
sheet = ss.insertSheet(folderName);
}
sheet.clear();
sheet.appendRow(["Name", "Date", "URL"]);
// Loop over folders in folder, using file iterator
while (contentfolders.hasNext()) {
var file = contentfolders.next();
var data = [
file.getName(),
file.getDateCreated(),
file.getUrl()
];
sheet.appendRow(data);
// Loop over files in folder, using file iterator
while (contentfiles.hasNext()) {
var file = contentfiles.next();
var data = [
file.getName(),
file.getDateCreated(),
file.getUrl()
];
sheet.appendRow(data)
}}
}
*For anyone interested this script lists both the Files and Folders in a Google Sheet based on the "specified folder"
I am trying to modify the below to code so the all files in the var fileBlob = e.parameter['file'+i]; are uploaded into the new sub-folder that is created.
After submitting the upload, a new folder is created for each file uploaded and each file is uploaded to each folder.
How can I get the doPost to submit the upload files to the new sub-folder only. So if I upload 20 files, 20 files are added/uploaded to the one sub-folder only.
Multi File Upload Ui Form
Google Drive Results
Code
function doPost(e) {
var foldername = e.parameter.folderName;
Logger.log('foldername = '+foldername);
var numFiles = Number(e.parameter.hidden);
Logger.log('numFiles = '+numFiles);
for (var i = 1; i<=numFiles; i++){
Logger.log(i);
var fileBlob = e.parameter['file'+i];
Logger.log(fileBlob);
var newFolder = DocsList.getFolderById("0Bw8NytPhOo00b0xIVm41LWlZRlk").createFolder(foldername);
newFolder.getUrl();
Logger.log(newFolder.getUrl());
var newFile = DocsList.createFile(fileBlob);
newFile.addToFolder(newFolder);
}
Full Code here (Created by Serge insas)
As mentioned in the other answer, the folder creation should happen just once before the loop but instead of adding the new file to the folder (and keeping it in the root folder as well unless you remove it) I would suggest to use the createFile method from the folder class instead.
The code becomes simpler :
function doPost(e) {
var foldername = e.parameter.folderName;
Logger.log('foldername = '+foldername);
var numFiles = Number(e.parameter.hidden);
Logger.log('numFiles = '+numFiles);
var newFolder = DocsList.getFolderById("0Bw8NytPhOo00b0xIVm41LWlZRlk").createFolder(foldername);
for (var i = 1; i<=numFiles; i++){
Logger.log(i);
var fileBlob = e.parameter['file'+i];
var newFile = newFolder.createFile(fileBlob);// create the file directly in the new folder
}
}
I think your problem here is that you call .createFolder for each file. Try putting
var newFolder = DocsList.getFolderById("0Bw8NytPhOo00b0xIVm41LWlZRlk").createFolder(foldername);
outside of the for loop.
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