1

With the help of a nice article (https://www.labnol.org/internet/google-drive-tree/21198/), I just setup my first google script in Google Drive.

Quick Question: How may I get a running script /myFolder1/music/myFirstScript.gs, to determine it's running in /myFolder1/music?

This didn't work. I got the Url but nothing in the log file showed the correct answer.

  var files = DriveApp.searchFiles('title contains "GoogleTreeAgenda5"');
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());
    Logger.log(file.getUrl()); 
    Logger.log(file.getDownloadUrl());
    Logger.log(file.getDescription());
    Logger.log(file.getOwner());
    Logger.log(file.getParents());
    Logger.log(file.getParents()[0].getName());
    Logger.log("------------------------------");
  }

Longer Behind The Scenes Reason:

The file will be modified a bit so that each month I will create a new tree structure off the root with agenda topics and the script will create an html file from the directory tree which is based on topic. Basically it will be an outline creator based on the current location of the script file. So Let's say this month it's in: /myFolder1/music (so in other words it's /myFolder1/music/myFirstScript.gs. The script needs to determine that folder is /myFolder1/music so that I can have it print the tree structure starting from the folder it's located in to an html file..

In the example application provided by the site I noted above, there are 2 options to print the tree from the google script:

1) Tree starting from the root folder

var parentFolder = DriveApp.getRootFolder(); 

2) Starting from a particular folder (but I can't figure out and will need "Folder_Name" to be determined dynamically from the location where the .gs file is located)

var parent = DriveApp.getFoldersByName("FOLDER_NAME").next(); 
Mickey D
  • 347
  • 2
  • 12

1 Answers1

1

I've used getScriptId() this will:

Gets the script project's unique id.

This id is also the unique id in the Google Drive. Then from there, create a recurring getParents() until you reach the root folder.

Here is the complete code:

function myFunction() {
var scriptID = ScriptApp.getScriptId();

//  Logger.log(scriptID)

// Log the name of every parent folder
 var driveFile = DriveApp.getFileById(scriptID); 
 var parentFolder = driveFile.getParents();
 while (parentFolder.hasNext()) {
   var folder = parentFolder.next();
   Logger.log(folder.getName());
 }  
}

NOTE:

There is no real or concrete path since a file can have multiple parents. It is also stated here in this related SO post.

Hope this helps.

Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • Thanks.. That worked for now but this Google Drive is a different world. Don't recall a script having multiple parents before. Got any references to how that works? Anyways thanks you answered my question and that will work for now because I don't think my script has multiple parents for now. – Mickey D Oct 19 '17 at 12:36
  • You can check the SO post in the note area also here is [comment from a GDE](https://stackoverflow.com/questions/41546503/how-to-get-absolute-path-of-a-document-and-file-from-google-drive-using-java-dri#comment70301074_41546503) and [forum](https://productforums.google.com/forum/#!msg/drive/h6OkYSrWZp0/cyjdTMZ7x8gJ) discussing file properties. – Mr.Rebot Oct 19 '17 at 13:50