GAS Library - CopyFolder

793 views
Skip to first unread message

Tanaike

unread,
May 25, 2020, 7:06:13 PM5/25/20
to Google Apps Script Community
This is Google Apps Script library for copying folder on Google Drive. I have sometimes the situation that it is required to back up the folder on Google Drive to Google Drive. But unfortunately, the method of "makeCopy()" of Class File and the method of "Files: copy" of Drive API cannot be used for directly copying the folder on Google Drive. So I created this as a library. This library can copy the folder on Google Drive. All files in the folder can be copied to Google Drive with keeping the folder structure. When there are the files with newer modified time in the source folder than those in the destination folder, the files in the destination folder are overwritten by the newer files in the source folder. Also, in this library, the folders in the shared Drive and the publicly shared folders can be used as the source and destination folder. If this was useful for your situation, I'm glad.


Darren D'Mello

unread,
May 25, 2020, 9:05:29 PM5/25/20
to google-apps-sc...@googlegroups.com
This is awesome. Thank you so much Tanaike

On Tue, May 26, 2020, 4:36 AM Tanaike <kanshi...@gmail.com> wrote:
This is Google Apps Script library for copying folder on Google Drive. I have sometimes the situation that it is required to back up the folder on Google Drive to Google Drive. But unfortunately, the method of "makeCopy()" of Class File and the method of "Files: copy" of Drive API cannot be used for directly copying the folder on Google Drive. So I created this as a library. This library can copy the folder on Google Drive. All files in the folder can be copied to Google Drive with keeping the folder structure. When there are the files with newer modified time in the source folder than those in the destination folder, the files in the destination folder are overwritten by the newer files in the source folder. Also, in this library, the folders in the shared Drive and the publicly shared folders can be used as the source and destination folder. If this was useful for your situation, I'm glad.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/bc08fcfc-9f1a-4c6c-a466-e45d7b460bdf%40googlegroups.com.

Fernando Falcao

unread,
May 26, 2020, 10:13:28 AM5/26/20
to google-apps-sc...@googlegroups.com
Wonderful! Very useful for me!!!
Thanks a lot,
Fernando

Em seg., 25 de mai. de 2020 às 20:06, Tanaike <kanshi...@gmail.com> escreveu:
This is Google Apps Script library for copying folder on Google Drive. I have sometimes the situation that it is required to back up the folder on Google Drive to Google Drive. But unfortunately, the method of "makeCopy()" of Class File and the method of "Files: copy" of Drive API cannot be used for directly copying the folder on Google Drive. So I created this as a library. This library can copy the folder on Google Drive. All files in the folder can be copied to Google Drive with keeping the folder structure. When there are the files with newer modified time in the source folder than those in the destination folder, the files in the destination folder are overwritten by the newer files in the source folder. Also, in this library, the folders in the shared Drive and the publicly shared folders can be used as the source and destination folder. If this was useful for your situation, I'm glad.


Tanaike

unread,
May 26, 2020, 7:57:26 PM5/26/20
to Google Apps Script Community
@miscellaneousmailer and @fernandojfalcao Thank you so much.

Clay Smith

unread,
Jun 6, 2020, 3:20:27 PM6/6/20
to google-apps-sc...@googlegroups.com
Hey Tanaike, 
I was trying out your library and I'm running into an issue. I have a nested folder structure but it only copies the top level. Any suggestions? 

   const object = {
      sourceFolderId: 'xxxx', // Please set the source folder ID.
      destinationFolderId: 'xxxx, // Please set the destination folder ID.
      overwrite: true,
    };
    console.log(object)
  const res = CopyFolder.copyAllFilesFolders(object);


Result:
{ numberOfCopiedFiles: 3,
  copiedFIles:
   [ { name: 'xxxxx',
       id: 'xxxxx' },
     { name: 'xxxxxxxxx', id: 'xxxx' },
     { name: 'xxxxxxxx',
       id: 'xxxxxxxxxxx' } ],
  message: 'Files were copied.',
  result: 'done' }

On Tue, May 26, 2020 at 7:57 PM Tanaike <kanshi...@gmail.com> wrote:
@miscellaneousmailer and @fernandojfalcao Thank you so much.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.


--

Tanaike

unread,
Jun 6, 2020, 7:45:59 PM6/6/20
to Google Apps Script Community
Thank you for your comment. I apologize for the inconvenience. Unfortunately, I cannot replicate your situation. Now when I tested your script, I could confirm that the script worked. I deeply apologize for this. By this, unfortunately, from your comment, I couldn't understand about the method for replicating your issue. So can you provide the detail flow for replicating your issue? By this, I would like to confirm it.

Kim Nilsson

unread,
Jun 8, 2020, 4:32:08 AM6/8/20
to Google Apps Script Community
@Tanaike,

do you have, or know of, a code snippet or library that would allow the possibility to pick/choose the two folders (a Drive folder picker?) instead of having to manually enter their fileIds?

Tanaike

unread,
Jun 8, 2020, 7:40:03 AM6/8/20
to Google Apps Script Community
Although this is not a library, for example, is this sample useful for your situation?

Kim Nilsson

unread,
Jun 8, 2020, 8:42:03 AM6/8/20
to Google Apps Script Community
Thank you.
That looks like something I can maybe figure out...
The goal is to not force the enduser to edit the copyFolder script at all. Just give them a copy of the Sheet, and have them run the menu

If I can have the getFiles function twice (getFiles1 and getFiles2) on the side menu (or maybe in a webapp (but side menu is fine too)), I can select both a source folder (id1) and a destination folder (id2).

Adding a Copy-button (in the side menu/webapp) to run the copyFolder function with the two values id1 and id2.

Is it possible easy to have two functions in the side menu, without the second function wiping all knowledge of the result from the first function?

Oh, I just noticed when testing, that it doesn't express the id for folders, only for files. Am I missing something?

Kim Nilsson

unread,
Jun 8, 2020, 8:43:43 AM6/8/20
to Google Apps Script Community
Oh, I added a tiny onOpen menu so the user can start the file picker without going to the script editor, as the goal is to help non-coder teachers. :-)

Kim Nilsson

unread,
Jun 15, 2020, 6:01:34 AM6/15/20
to Google Apps Script Community
@Tanaike,

I never got a response to my question about you little side menu never displaying the ID of folders, only files.

Tanaike

unread,
Jun 15, 2020, 7:32:24 PM6/15/20
to Google Apps Script Community
Thank you for your comment. I deeply apologize that I cannot answer for all questions. This is due to my poor skill. I would like to study more and more.

2020年6月15日月曜日 19時01分34秒 UTC+9 Kim Nilsson:

Alan Wells

unread,
Jun 15, 2020, 7:41:34 PM6/15/20
to Google Apps Script Community
Tanaike, your skills are very poor, . . . compared to God.  Haha!
But other than that, I think that your skills are relatively good.

Tanaike

unread,
Jun 15, 2020, 8:04:46 PM6/15/20
to Google Apps Script Community
@aj.addons Thank you for your comment. I'm honored for it. Your replying save me. I think that I always want to answer various questions and to resolve various issues as possible as I can. When I cannot answer and resolve a question and an issue, I would feel terribly sorry about it. At that time, I think that I have to study more. This might grow me. But when I cannot answer, I'm depressed. But now, I was saved by your replying. Thank you so much.

Alan Wells

unread,
Jun 15, 2020, 8:29:58 PM6/15/20
to Google Apps Script Community
The brain is a negative assessment machine.  In a hostile environment, that keeps living things alive.  But, we aren't living in the wild anymore, running from flesh eating dinosaurs.  Haha!  So, it is easy for the brain to evaluate to the most negative outcome, in anticipation of what is needed for a defense.  Unfortunately, that "script" in our brains runs at times when it's not really needed, or applicable.  So, no need to feel bad.

Tanaike

unread,
Jun 15, 2020, 8:39:40 PM6/15/20
to Google Apps Script Community
Thank you for your support. I would like to keep in mind your advices and to move on. Thank you very much.

Michael O'Shaughnessy

unread,
Jun 15, 2020, 9:30:51 PM6/15/20
to google-apps-sc...@googlegroups.com
Well said Alan Wells!!!

On Mon, Jun 15, 2020 at 8:39 PM Tanaike <kanshi...@gmail.com> wrote:
Thank you for your support. I would like to keep in mind your advices and to move on. Thank you very much.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

A Park

unread,
Jun 16, 2020, 6:32:10 AM6/16/20
to Google Apps Script Community
@Kim Nilsson - a partial solution I have used for non-coders is to use a Google Sheet as a UI. If I need a folder ID, I generally request that they paste the folder URL (most can figure out how to select+copy+paste from the address bar) then I have som Google Apps Script that grabs that from the Sheet, parses out the folder ID and uses that in the script.

I have not done this with Tanaike's script but I will be testing out the script later today or tomorrow for something I need to automate and can throw this together pretty quickly if it helps (I have all of the necessary code already and just need to repurpose it for this case).

Adrian

Kim Nilsson

unread,
Jun 16, 2020, 6:50:36 AM6/16/20
to Google Apps Script Community
Yes, that works too. Less advanced coding needed, which I like. :-)
And a simple process for the user that needs to copy a folder structure.

Share the Sheet with a Copy-link to the user.
Have the Sheet contain descriptive text, informing the user how to copy the FolderID and paste it in cell A7 (for example) and then the script pulls the id from that cell, instead of needing to edit the script. And a simple menu (or a button) to run the actual copy function.

Kim

Kim Nilsson

unread,
Jun 16, 2020, 6:57:56 AM6/16/20
to Google Apps Script Community
Ahh, also totally forgot that I had once seen a web app only solution.

kle...@ritenourschools.org

unread,
Feb 19, 2021, 8:01:56 PM2/19/21
to Google Apps Script Community
Can you share your code. .I am just getting started and have several GAS 'projects' that generate forms from spreadsheets. I want to make them available to others. I do not understand the environment as I come from the mainframe world. Question: do you have to program a picker into every project or is there a way to connect to the equivalent of a 'subroutine?  I am thinking the non-coding teacher should be able to select the spreadsheet to hold responses from a form. I have code that will create a new form for responses in the same folder or a response sheet in the same spreadsheet. So far none of my attempts to recreate any picker code I have found have worked. I am a newbie for certain. Thank you for any help you can give. L.Klein

Kim Nilsson

unread,
Feb 20, 2021, 6:35:02 AM2/20/21
to Google Apps Script Community
Google Forms has a built-in feature for selecting which Sheet to receive the responses.
No need to code anything.

Kim

Lissa Klein

unread,
Feb 20, 2021, 10:45:50 AM2/20/21
to google-apps-sc...@googlegroups.com
I am working on a bound spreadsheet script that builds a form and lets the user choose to create the response sheet in the same spreadsheet as the source of questions sheet, in the same folder or in an existing spreadsheet anywhere on their drive. I have given up on making the picker work and am using Tanaikel's 'sample script for selecting files in Google Drive using HTML select box for Google Apps Script.' https://gist.github.com/tanaikech/96166a32e7781fee22da9e498b2289d0
I don't recognize some of the code. What are the $ in front of things?  What documentation should I look for to understand.
<script>
var alldata = {};

$(function() {
  const rootFolderId = "root";
  google.script.run.withSuccessHandler(importData).getFiles(rootFolderId, rootFolderId);

  $('#select_file').change(function() {
    var id = $('#select_file').val();
    var disp = $('#select_file option:selected').text();
    if (~disp.indexOf("Folder") || ~disp.indexOf("../")) {
      $('#select_file > option').remove();
      if (alldata[id]) {

He has this in his HTML

The picker is much more attractive but I have been trying for two weeks and none of the examples I created work!  I know part of the problem is I was trying to create an add-on (which I have never done and do not understand) at the same time. Now I hear about libraries. That warms my mainframe heart. If I could put a file picker in a library it could be included in many projects, not hardcoded in every one with attendant maintenance issues.

L.Klein



--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/2X2NAeGeZYA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CACK38a4K0T3ECfX3%2B_tdU927Dz-%3DCVE6jBbN6c%2BrQZhNX_4Lcg%40mail.gmail.com.


--
L.Klein

CONFIDENTIALITY NOTICE: Any information contained in or attached to this message is intended solely for the use of the intended recipient(s). Any review, retransmission, dissemination of, or taking of any action in reliance upon, this communication by persons other than the intended recipient(s) is prohibited.  If you are not the intended recipient or a person responsible for delivering this message to the intended recipient(s), reply to sender to inform us that you have received this message in error and destroy all copies of the original message. Please do not copy, distribute or use this email or the information contained in it for any purpose without permission of the sender.

Kim Nilsson

unread,
Feb 20, 2021, 10:55:48 AM2/20/21
to Google Apps Script Community
Ah, well, you have already surpassed my skills.
I have no answer. 

Alan Wells

unread,
Feb 20, 2021, 12:02:20 PM2/20/21
to Google Apps Script Community
The dollar sign at the front is for jQuery, which is a JavaScript library.
jQuery uses "$" as a shortcut for "jQuery".
Example :
$("#id") or jQuery("#id") are both same. 
Many javascript libraries use the $ as the primary base object.
So, it's not exclusive to jQuery.
That $ single dollar sign character is actually a valid variable name.
These dollar signs are part of the jQuery syntax.
To access any of the jQuery methods, you first need to type a dollar sign or jQuery.
Your Apps Script library works the same way.
You need to designate an identifier in order to access the code in the library.

Steve Waters

unread,
Jun 11, 2023, 8:31:30 AM6/11/23
to Google Apps Script Community
Sorry raise the dead... @clay did find an option for coping a nested structure in the end?

Clay Smith

unread,
Jun 11, 2023, 10:45:06 AM6/11/23
to google-apps-sc...@googlegroups.com
Hey Steve, yes and no. 
I had to build my own system in Node.js. It can be done in apps script I just needed something more robust. I mapped the structure as I copied and recorded the file parent to the copied file as I went along through a recursive function. Then nested back together. 

What are you looking to do?  

On Jun 11, 2023, at 08:31, Steve Waters <steve....@certane.com> wrote:

Sorry raise the dead... @clay did find an option for coping a nested structure in the end?

Disclaimer
This email is intended only for the use of the individual or entity named above and may contain information that is confidential and privileged. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this email and/or its attachments is strictly prohibited. Any views expressed in this message are those of the individual sender and may not necessarily reflect the views of Certane or any subsidiary or affiliate that may be relevant to the message (collectively the “Companies”). The Companies accept no responsibility for the content of any email which is sent by an employee which is of a personal nature.  

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages