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 C:\Users\Coyote\Parties\NewYears\muffy\topless\pic_1.jpg IDL> shortname = StrMid( fullname, StrLen(thePath) ) IDL> Print, shortname pic_1.jpg
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.
Copyright © 1997-2005 David W. Fanning
Last Updated 23 September 2005