Re: extracting all but... [message #46300] |
Sat, 19 November 2005 15:12 |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
David Fanning wrote:
> Reimar Bauer writes:
>
>> idl has in idl6.0 introduced two routines FILE_BASENAME and FILE_DIRNAME.
>> There is no builtin routine to separate the extension - unfortunately.
>
> Well, I'll offer up FSC_BASE_FILENAME, which can give you the
> directory, the filename, and the extension all in one go:
>
> ; EXAMPLE:
> ;
> ; IDL> thePath = "C:\rsi\idl7.8\lib\jester.pro"
> ; IDL> Print, FSC_Base_Filename(thePath, Directory=dir,Extension=ext)
> ; jester
> ; IDL> Print, dir
> ; C:\rsi\idl7.8\lib\
> ; IDL> Print, ext
> ; pro
>
> You can find it here:
>
> http://www.dfanning.com/programs/fsc_base_filename.pro
>
> Cheers,
>
> David
David,
your routine does not get the extension on this example in opposite of Theos
file_path_name_ext which works on windows and unix.
FSC_Base_Filename drops the dot :-(
IDL> thePath = '../.profile'
IDL> Print, FSC_Base_Filename(thePath, Directory=dir,Extension=ext)
% Compiled module: FSC_BASE_FILENAME.
% Compiled module: PATH_SEP.
% Compiled module: STRSPLIT.
profile
IDL> fpe=file_path_name_ext(thePath)
% Compiled module: FILE_PATH_NAME_EXT.
% Compiled module: REPLACE_STRING.
IDL> print,fpe.path
../
IDL> print,fpe.name
IDL> print,fpe.ext
.profile
cheers
Reimar
|
|
|
Re: extracting all but... [message #46303 is a reply to message #46300] |
Sat, 19 November 2005 08:29  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Reimar Bauer writes:
> idl has in idl6.0 introduced two routines FILE_BASENAME and FILE_DIRNAME.
> There is no builtin routine to separate the extension - unfortunately.
Well, I'll offer up FSC_BASE_FILENAME, which can give you the
directory, the filename, and the extension all in one go:
; EXAMPLE:
;
; IDL> thePath = "C:\rsi\idl7.8\lib\jester.pro"
; IDL> Print, FSC_Base_Filename(thePath, Directory=dir,Extension=ext)
; jester
; IDL> Print, dir
; C:\rsi\idl7.8\lib\
; IDL> Print, ext
; pro
You can find it here:
http://www.dfanning.com/programs/fsc_base_filename.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: extracting all but... [message #46304 is a reply to message #46303] |
Sat, 19 November 2005 07:59  |
William Daffer
Messages: 34 Registered: February 1999
|
Member |
|
|
kl_tah@hotmail.com writes:
> Hi All,
>
> I was wondering how I can extract all but a constant string from an
> array of strings?
> basically, I have an array of filenames with a particular directory
> prefixed to each of the filenames like so:
>
> files =
> ['/some/constant/directory/file1','/some/constant/directory/ file2',
> etc.]
>
> and would like to have just an array of the filenames without the
> prefixed directory name so that the result looks like:
>
> files = ['file1', 'file2', etc.]
>
>
> any ideas?
You didn't tell which version of IDL you're using, so I'm going to
assume it's a fairly recent version.
If it's just a matter of removing the path, use File_Basename()
which, like the unix utility `basename', returns the last component
of a path, i.e. the file name itself.
file_basename('/some/constant/directory/file1') == 'file1.'
You don't have to loop over the filenames, just do the whole vector at once.
the_part_you_want=file_basename(files)
If the constant part of path isn't the entire path and you know its
value, you can use STREGEX()
parts = stregex(your_files,'/the/constant/part/(.*)$', $
/extract,/subexp)
the_parts_you_want=parts[1,*]
You might want to 'reform()' that, to make the first dimension go
away.
If the part isn't constant, you're on your own. ;-)
whd
--
PAINTING, n. The art of protecting flat surfaces from the weather and
exposing them to the critic.
Formerly, painting and sculpture were combined in the same work:
the ancients painted their statues. The only present alliance between
the two arts is that the modern painter chisels his patrons.
-- Ambrose Bierce: _The Devil's Dictionary_
|
|
|
Re: extracting all but... [message #46305 is a reply to message #46304] |
Sat, 19 November 2005 01:46  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
kl_tah@hotmail.com wrote:
> Hi All,
>
> I was wondering how I can extract all but a constant string from an
> array of strings?
> basically, I have an array of filenames with a particular directory
> prefixed to each of the filenames like so:
>
> files =
> ['/some/constant/directory/file1','/some/constant/directory/ file2',
> etc.]
>
> and would like to have just an array of the filenames without the
> prefixed directory name so that the result looks like:
>
> files = ['file1', 'file2', etc.]
>
>
> any ideas?
>
> Cheers,
> KL
You could use our file_path_name_ext routine from our library
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/file_path_name_ext_dbase.pro.html
more routines are described here:
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
idl has in idl6.0 introduced two routines FILE_BASENAME and FILE_DIRNAME.
There is no builtin routine to separate the extension - unfortunately.
file_path_name_ext returns a structure of path,name,ext input could be a
scalar or an array
cheers
Reimar
|
|
|
Re: extracting all but... [message #46306 is a reply to message #46305] |
Fri, 18 November 2005 18:50  |
guillaume.drolet.1
Messages: 20 Registered: July 2005
|
Junior Member |
|
|
kl_tah@hotmail.com a écrit :
> Hi All,
>
> I was wondering how I can extract all but a constant string from an
> array of strings?
> basically, I have an array of filenames with a particular directory
> prefixed to each of the filenames like so:
>
> files =
> ['/some/constant/directory/file1','/some/constant/directory/ file2',
> etc.]
>
> and would like to have just an array of the filenames without the
> prefixed directory name so that the result looks like:
>
> files = ['file1', 'file2', etc.]
>
>
> any ideas?
>
> Cheers,
> KL
Dear KL,
As a solution, I would use the 'STRSPLIT' function with the /EXTRACT
keyword and '/' as a delimiter. It will return a string array
containing 4 string: [some constant directory file2].
For example, inside a loop:
string_arr = strsplit(files[i],'/',/extract)
Then it' easy to get the last element of the array and put it in
another array. I would repeat the operation for all your path+file.
Gui
|
|
|