Strip a filename from a path

I am brain dead today !

Have a path to a file which has been returned using an open file dialogue from the example below :

var file_upload = application.showFileOpenDialog()

An example of the string held within the variable is :

‘/Users/harry/Documents/document.pdf’

Can anybody provide a simple way to get the filename (i.e. ‘document.pdf’ ) from that string ?

Remember that the filename will change each time the open file dialogue is used to point to a particular file !

Thanks
Harry

under plugins/file you find a getParent function that will return /Users/harry/Documents. If you substitute that string plus ‘/’ in your path by ‘’, you have your doc name.

Thanks, Patrick

I will try that solution later.

I did play with this late yesterday evening and got my desired result using the following to extract the filename :

//choose the file to be uploaded
var file_upload = application.showFileOpenDialog()

//find the position of the last oblique in the file path
//add 1 to it to return the beginning position of the file name
var pathLastSlash = file_upload.lastIndexOf(“/”) + 1

//calculate the string length of filename
var len_filename = file_upload.length - pathLastSlash

//get the file name using the ‘stringRight()’ function
var file_2_upload = utils.stringRight(file_upload, len_filename)

Any feedback on whether the ‘lastIndexOf()’ function is good to use in this instance ?

Harry

Seems like a logical choice.

You can do these things in a zillion ways.

var x = "ddfdf/dfdfdf/aaaapdf"
var div = x.lastIndexOf("/")+1
var y = x.substring(div,x.length) //start end

Thanks Maarten,

I like the ‘substring()’ function - I didn’t know it was there until now !

Incidentally I am having problems testing the getParent() function offered by Patrick and get errors running the method.

Debugger tells me that getParent is not a function !

Tred a variety of entry formats but obviously NOT the right one !

Example method out of the many variations :

var file_upload = “/users/harrycat/desktop/ftp.pdf” ;

var filePath = file_upload.getParent()

Anybody put me out of my misery, please ?

Harry

You’re trying to trigger a function .getParent() on a String object file_upload

When you look in the Plugins/file/JSfile node you will see all the functions that will work with a JSFile object. Also .getParent()

When you look in the Plugins/file node you will see the function plugins.file.convertStringToJSFile()
Hover over this function and you will see details of what the function returns.(see attached image)

So your code should be like this:
var file_upload = “/users/harrycat/desktop/ftp.pdf” ;
var FSfileUploadObject = plugins.file.convertStringToJSFile(file_upload)//returns JSFile Object
var filePath = FSfileUploadObject.getParent() //returns StringObject

Thanks, once again, Maarten for shining some light into a foggy brain.

Having read your explanantion I went to play with the JSFile functions and found ANOTHER one of which I was unaware :

‘getName()’

If I modify my method to :

var fileUpload = “/users/harrycat/desktop/ftp.pdf”
var fileFullPath = plugins.file.convertStringToJSFile(fileUpload)
var getNameOfFile = fileFullPath.getName()

Then I get ‘ftp.pdf’ returned without any of the parsing gymnastics needed from the use of length() and substring() functions !

As you rightly say, there are a zillion ways to skin this particular cat but are there any gotchas to using ‘getName()’ ??

The documentation is a little sparse on File Plugin functions (unless I am looking in the wrong place) and how the File & the JSFile nodes interact.

There is also my relative inexperience in knowing how the different functions operate.

Any info source that anybody could point me to which may help would be much appreciated

Thanks
Harry

Turns out it’s as simple as this:

var vAttachPath = plugins.file.showFileOpenDialog();
var vFilename = vAttachPath.getName();

Thanks Harry, for pointing out this older thread.