How do I locate the path of the current folder? I just want to be able to get the path of the folder so that I can manipulate the files in the folder without typing the path into the scripts.
-
Do you have a standalone script or something that is embedded in a Spreadsheet ? – Srik Feb 08 '13 at 10:49
7 Answers
For a Spreadsheet I found this to work:
thisFileId = SpreadsheetApp.getActive().getId();
var thisFile = DriveApp.getFileById(thisFileId);
var parentFolder = thisFile.getParents()[0].getName();
-
1For a Document use: thisFileId = DocumentApp.getActiveDocument().getId(); instead. – savedario Nov 15 '14 at 14:34
-
@savedario if only there was some way to get the active no matter what kind it was... – Michael Mar 28 '18 at 19:29
Add a function like this to your script
function getThisScriptInDrive() {
return DriveApp.find("some unique string that wont be anywhere else")[0];
}
This will search Drive and find this script itself because it contains that string - right there in the function call! - no need to declare it anywhere else. As long as you use an obscure enough string - i'd recommend mashing a few hundred chars on your keyboard - it will be unique across drive and therefore just work.
Once you have a File for this script, you can call getParents() etc.
Thanks to Corey's answer and to Thomas'one, here is a "full featured" version that shows the folder tree in the logger and every parents id as well... just for fun ;-)
function getScriptFolderTree() {
var thisScript = getThisScriptInDrive();
var names = []
var Ids = []
var folder = thisScript.getParents()[0];
while (folder.getName() != "Root"){
names.unshift(folder.getName());
Ids.unshift(folder.getId());
var parents = folder.getParents();
var folder = parents[0];
}
Logger.log('Root/'+names.join().replace(/,/g,'/'))
Ids.unshift(DriveApp.getRootFolder().getId())
Logger.log(Ids)
}
function getThisScriptInDrive() {
return DriveApp.getFileById("poiuytrezazertyujhgfdsdcvcxyydryfhchfh");
}
(ID's are truncated intentionally)
Note that this script is working nicely but it strangely stops working if the 'random string' is modified... I imagine that the search engine in drive doesn't like the change but I have no serious explanation (comments welcome) but after a few minutes it works again ;-)

- 4,921
- 5
- 37
- 53

- 45,904
- 7
- 105
- 131
-
Procrastination pays. Thanks Serge. You saved me some time. The delay is probably caused by all the indexing. – ScampMichael Feb 09 '13 at 00:53
-
Hi Michael, you're right about indexing.... strangely today it is almost instantaneous ;-) - That said, the script is not yet perfect, some "special cases" where a file is in 2 folders simultaneously (which you can't make with the Drive web UI but is feasible with GAS) are not handled correctly... Anyway, I'm not concerned by this special case as I prefer not to have these kind of weird arrangement ^^ – Serge insas Feb 09 '13 at 22:48
-
Hi Serge, It is possible to make one folder the child of 2 other folders. see:http://webapps.stackexchange.com/questions/26010/with-google-drive-how-can-i-have-a-single-document-in-multiple-folders-collecti So this script is FAR from perfect. – Thomas Feb 12 '13 at 13:09
-
Thanks for the link, I didn't know the 'command click' selection... even if I'm pretty sure I won't ever use it :-) - That said, feel free to suggest a better version that handles every possible combination, I'll be happy to vote it up. – Serge insas Feb 12 '13 at 15:35
-
What random string? Is poiuytrezazertyujhgfdsdcvcxyydryfhchfh a random string or the id of the script or the spreadsheet that is bound to the script or the form id that is bound to the spreadsheet or ????? – aNewb Feb 05 '21 at 17:39
I tweaked Serge's code to write a generic function
function getThisFilePath() {
//let thisFile = DriveApp.getFileById(DocumentApp.getActiveDocument().getId()); // for a Doc
let thisFile = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()); // for a Sheet
let path = {name: '', folder: []};
let folder = thisFile.getParents().hasNext() ? thisFile.getParents().next() : undefined;
while (folder) {
path.folder.unshift({name: folder.getName(), id: folder.getId()});
folder = folder.getParents().hasNext() ? folder.getParents().next() : undefined;
}
path.folder.forEach(folder => path.name += '/' + folder.name);
Logger.log(path.name);
return path;
}
Logging output
11:02:57 AM Info /My Drive/Technology/Development/2022

- 41
- 4
You could do this:
function myFunction() {
var thisScript = getThisScriptInDrive();
var folder = thisScript.getParents()[0];
while (folder.getName() != "Root"){
var parents = folder.getParents();
for (var i in parents){
var folder = parents[i];
Logger.log(folder.getName());
}
}
}
function getThisScriptInDrive() {
return DocsList.find("`<jj!?=(<DW+.W/m7SBF:sgu/@B(&Cs3:{ajA~ys@KmN4&]ujhpZ~z[Tv?+dk}MpK,8pY=w&dny8N'74:.9H:~uCgY=7pRt4[Tn5")[0];
}
This only works good if the folder has only 1 parent because it only takes 1 path.
edit: Thanks to Corey G
-
1Thank a lot for your reply. How do I get the FOLDER ID of the folder in which the script file is located? – user2054013 Feb 08 '13 at 15:39
-
I was able to get the folder containing the script that was running:
//
// PrintScriptFolder -- print the name of the folder in which the running script resides.
//
function PrintScriptFolder()
{
var scriptId = ScriptApp.getScriptId();
console.info('scriptId = ' + scriptId);
var file = DriveApp.getFileById(scriptId);
var folders = file.getParents();
if (folders.hasNext())
{
var folder = folders.next();
var name = folder.getName();
console.info('script folder name = ' + name);
}
}
The trick is using ScriptApp to get the ID of the running script. From there it's pretty straightforward: use DriveApp to get the file ID, getParents() to get a list of (one) parent folder, next() to get it, and getName() to get its name.

- 1
- 1
-
scriptId = 1xe2mbAXd1M8djvRFS_xlN8hG8g1B4C3UuRVxEa4vTQ7dQMe3NlxlYUIC Feb 5, 2021, 11:34:13 AM Error Exception: No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it. Just your function running from my script!?!? – aNewb Feb 05 '21 at 17:35
First answer, long time lurker (be kind ;-)
Looking for a simple getPath, I went for a more generic approach, with limitations:
//simple test stub
function test_getFilePath() {
const scriptId = ScriptApp.getScriptId();
const fullPath = getFilePath(scriptId);
console.log(fullPath);
}
/**
* getFilePath
* @fileId id of file to find path for
*
* NB: google allows for multiple parents, which is a PITA
* so I'm just taking the first one I find up the tree
*/
function getFilePath(fileId) {
const file = DriveApp.getFileById(fileId);
const rootFolder = DriveApp.getRootFolder()
const rootFolderId = rootFolder.getId();
const rootFolderName = rootFolder.getName();
let folder = file.getParents().next();
let folderNames = [];
while (folder.getId() !== rootFolderId){
folderNames.unshift(folder.getName());
folder = folder.getParents().next();
}
const fullPath = rootFolderName+'/'+folderNames.join().replace(/,/g,'/');
return fullPath;
}
console:
11:36:40 AM Info My Drive/GAS
Had I more time/inclination, I'd have writen a recursive function to capture and return an array of possible parent paths, but there's no use case for me and who the hell wants to keep a the same file in multiple folders anyway?? (I'm sure there are use cases, eg, equivalent to using symlinks, but it stil makes me feel dirty ;-)

- 1
- 1