1

I am using the following code in order to retrieve sub folder names from the path declared. This works fine but how do I then remove the path name so that the array is a list of just folder names?

var myPath = Folder ("Z:/My File System/Me/Work Files/Design");
var folders = getFolders (myPath); 

function getFolders(sourceFolder) {
var folderArray = new Array();
var sFolders = sourceFolder.getFiles ();
var len = sFolders.length;
    for (var i = 0; i < len; i++) {
    var sFolder = sFolders[i];
        if (sFolder instanceof Folder) {
            folderArray.push(sFolder);
        }
    }
return folderArray;
}

Instead of returning:

Z:/My File System/Me/Work Files/Design/One
Z:/My File System/Me/Work Files/Design/Two
Z:/My File System/Me/Work Files/Design/Three
Z:/My File System/Me/Work Files/Design/Four

I need:

One
Two
Three
Four

dimmech
  • 827
  • 9
  • 19
  • How to get the path from a `Folder` instance? – Bergi Oct 14 '13 at 16:19
  • See http://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript – Dan Goodspeed Oct 14 '13 at 16:20
  • possible duplicate of [How to pull the file name from a url using javascript/jquery?](http://stackoverflow.com/questions/1302306/how-to-pull-the-file-name-from-a-url-using-javascript-jquery) – Bergi Oct 14 '13 at 16:21
  • Although I appreciate the answers given, I don't get how to incorporate the solutions into the for loop I gave. Answer 1 seems like it would be a process of creating the string on each iteration and then split it? a bit confused on how to do that. Other answers point to Regexp. Which, if I got the expression right to begin with, I would still need to understand how to incorporate it into a for loop. – dimmech Oct 14 '13 at 17:56

3 Answers3

1

You could use split() like this, assuming there are no other slashes towards the end.

var sample = 'Z:/My File System/Me/Work Files/Design/Four'.split('/')
var result = sample[sample.length - 1]
Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43
1

You could implement something like this (assuming you can modify the Folder prototype, and it stores the path as this.path):

Folder.prototype.basename = function () {
    return this.path.split('/').pop();
};

You would then append the base names to the array:

folderArray.push(sFolder.basename());
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

Regexp the string. Start at the end, and work backward until the first '/'

caleb.breckon
  • 1,336
  • 18
  • 42