Friday, March 14, 2014

Don't forget to toString() or fsName your file paths

I've had this problem several times recently, where I was trying to use a string function on a file path, and kept getting an error that something "was not an object." I was trying to use lastIndexOf("/") on the path so I could grab just the file name from the end of the path.

var myFileName = fileList[i];       
  var myIndex = myFileName.lastIndexOf('/');

//error


Anyway, the problem was that the file path as I had grabbed it was not really a string, even though it looks like one. I needed to use toString() on it before using lastIndexOf() and then all was well.

var myFileName = fileList[i].toString();       
 var myIndex = myFileName.lastIndexOf('/');

Then I remembered "fsName." I've used it in my other scripts, but I thought it just changed the ugly "%20" code back into spaces. It turns out that it also turns the path into a string. So you get two for one.

var myFileName = fileList[i].fsName();       
 var myIndex = myFileName.lastIndexOf('/');

No comments:

Post a Comment