Fanning Software Consulting

Acquiring the File Name from the File Path

QUESTION: Usually, I use Dialog_Pickfile for file selection. But I want to use the selected file name in another procedure. If I print the selection that is returned from Dialog_Pickfile, it is the fully-qualified file path along with the file name. Is there a trick for extracting just the file name?

ANSWER: I'm not sure it is a trick, but I would do something like this:

   IDL> fullname = Dialog_Pickfile(Get_Path=thePath)
   IDL> Print, fullname
   IDL> shortname = StrMid( fullname, StrLen(thePath) )
   IDL> Print, shortname

Of course, if you were lucky enough to have IDL 6.0 or higher, you could do the same thing with FILE_BASENAME. (The related routine, FILE_DIRNAME, can be used to obtain the directory name.)

In my own work, I often like to parse paths like this to obtain the directory, the base filename, and the file extension. I wrote my own program to do this all in one go. It is called cgRootName.

Web Coyote's Guide to IDL Programming