Hello,
I been down this street before. Try using the PARSE_FILENAME() function
below. I also added a sort_extension rotuine which worked on the files
called Untitled.01, Untitled.12, untitled.03
Here's the output of sort_extension:
IDL> sort_extension
Macintosh HD:pemaquid:untitled.01has extension 01
Which is a float value of 1.00000
And is position 0 in the file list
Macintosh HD:pemaquid:untitled.03has extension 03
Which is a float value of 3.00000
And is position 1 in the file list
Macintosh HD:pemaquid:untitled.12has extension 12
Which is a float value of 12.0000
And is position 2 in the file list
+++++START
;+
; NAME:
; PARSE_FILENAME
;
; PURPOSE:
; This function returns a 3 column array of a parsed filename.
; Each column contains the path, filename and extension respectively.
;
; CATEGORY:
; String processing.
;
; CALLING SEQUENCE:
; result = PARSE_FILENAME(Filename)
;
; INPUTS:
; Filename : a string or array of strings of the complete path and
; extension to a file.
;
;
;
; OUTPUTS:
; This function returns a 3,n string array. the number of rows is
determined
; by the number of elements in the input parameter.
; F = PICKFILE(/READ, FILTER = ['pro', 'dat'])
;
; RESTRICTIONS:
; Directories are divided by a forward slash (/) in UNIX or a
; backslash (\) in WINDOWS or a colon (:) in MACOS.
; Three character file extensions are appended with a dot (.).
; MODIFICATION HISTORY:
; Written by: Ben Tupper, August, 1999
;
; 26 SEP 99 Increased Dot position by one
; 18 JAN 00 Corrected how the FileName is extracted
; when there is no '.ext'
;-
FUNCTION parse_filename, filelist
OS = StrLowCase( !version.os_family)
case os of
'unix': separator = '/'
'windows': separator = '\'
'macos': separator = ':'
endcase
n = n_elements(filelist) ; get the number of files that qualify
If n LT 1 Then Return,-1
slash = rstrpos(filelist,separator) +1 ; get the last slash position plus
one
dot = strpos(filelist,'.') ; get the dot position
len = dot - slash ; calc the length of the name
parsed = strarr(3,n)
;get the filename
for i = 0,n-1 do $
If Dot[i] NE -1 Then parsed[1,i] = strmid(filelist[i],slash[i], len[i])
Else $
Parsed[1,i] = StrMid(filelist[i],slash[i])
len = slash ;get the path
for i = 0,n-1 do parsed[0,i] = strmid(filelist[i],0, len[i])
len = strlen(filelist) - dot-1
; get the extension if it exists
For i = 0,N-1 Do Begin
If Dot[i] NE -1 Then $
parsed[2,i] = strmid(filelist[i],dot[i]+1, len[i]) $
Else $
parsed[2,i] = Strmid(filelist[i],Slash[i], len[i] - Slash[i])
EndFor
return, parsed
end
PRO SORT_Extension
Files = FindFile('Macintosh HD:pemaquid:untitled*')
Parsed = Parse_FileName(Files)
Floated = Float(StrTrim(Parsed[2,*],2))
Sorted = Sort(Floated)
For i = 0, N_elements(Files)-1 Do Begin
Print, Files[i] + 'has extension ' + Parsed[2,i]
Print, 'Which is a float value of ', Floated[i]
Print, 'And is position ', Sorted[i], ' in the file list'
EndFor
END
++++++END
Simon de Vet wrote:
> Trivial problem, no doubt, but I've never been able to get my brane
> around formatting options.
>
> I have a list of files, with the extension .01, .02, .03 ... .12. The
> preceding portion is indentical in all cases.
>
> I'd like to be able to load these in a loop, but I first need to convert
> the counter value into this format.
>
> My current, and very ugly, technique is to have an array filled with
> ['01', '02', ... '12'], and then reference the appropriate entry. It
> works, but I don't like it.
>
> Any ideas?
>
> Simon
--
Ben Tupper
Bigelow Laboratory for Ocean Science
West Boothbay Harbor, Maine
btupper@bigelow.org
note: email address new as of 25JULY2000
|