comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Open text files consecutively without knowing names
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Open text files consecutively without knowing names [message #85264] Fri, 19 July 2013 09:22 Go to next message
morganlsilverman is currently offline  morganlsilverman
Messages: 46
Registered: February 2013
Member
Hello,

I have several folders containing ascii files in each. The filenames within those folders are a grouping of numbers and letters corresponding to year, day, and other instrument characteristics
(e.g 8358d47a.tps, 8358h41a.tps, 8358l35a.tps). Unfortunately there isn't a pattern or order to the filenames beyond the first couple digits which are connecting to the parent directory to easily open the files consecutively.

I want to open each file, read the data, then close and repeat on the next file. Is there a way to loop through the files in each directory without knowing specifically what the filenames are? I'm struggling with how to do this since.

Thank you. Sincerely,
Morgan
Re: Open text files consecutively without knowing names [message #85265 is a reply to message #85264] Fri, 19 July 2013 09:42 Go to previous messageGo to next message
John Correira is currently offline  John Correira
Messages: 25
Registered: August 2011
Junior Member
On 07/19/2013 12:22 PM, Morgan Silverman wrote:
> Hello, I have several folders containing ascii files in each. The
> filenames within those folders are a grouping of numbers and letters
> corresponding to year, day, and other instrument characteristics (e.g
> 8358d47a.tps, 8358h41a.tps, 8358l35a.tps). Unfortunately there isn't a
> pattern or order to the filenames beyond the first couple digits which
> are connecting to the parent directory to easily open the files
> consecutively. I want to open each file, read the data, then close and
> repeat on the next file. Is there a way to loop through the files in
> each directory without knowing specifically what the filenames are?
> I'm struggling with how to do this since. Thank you. Sincerely, Morgan


Should be something like this:

files = file_search('/path/to/your/files/','*.tps', COUNT=nfiles)

for i=0, nfiles-1 do begin

nlines = FILE_LINES(files[i])
data = STRARR(nlines)
OPENR, lunit, files[i], /get_lun
READF, lunit, data
CLOSE, lunit
FREE_LUN, lunit

; Do some stuff here

endfor
Re: Open text files consecutively without knowing names [message #85266 is a reply to message #85265] Fri, 19 July 2013 10:17 Go to previous messageGo to next message
morganlsilverman is currently offline  morganlsilverman
Messages: 46
Registered: February 2013
Member
On Friday, July 19, 2013 12:42:05 PM UTC-4, John Correira wrote:
> On 07/19/2013 12:22 PM, Morgan Silverman wrote:
>
>> Hello, I have several folders containing ascii files in each. The
>
>> filenames within those folders are a grouping of numbers and letters
>
>> corresponding to year, day, and other instrument characteristics (e.g
>
>> 8358d47a.tps, 8358h41a.tps, 8358l35a.tps). Unfortunately there isn't a
>
>> pattern or order to the filenames beyond the first couple digits which
>
>> are connecting to the parent directory to easily open the files
>
>> consecutively. I want to open each file, read the data, then close and
>
>> repeat on the next file. Is there a way to loop through the files in
>
>> each directory without knowing specifically what the filenames are?
>
>> I'm struggling with how to do this since. Thank you. Sincerely, Morgan
>
>
>
>
>
> Should be something like this:
>
>
>
> files = file_search('/path/to/your/files/','*.tps', COUNT=nfiles)
>
>
>
> for i=0, nfiles-1 do begin
>
>
>
> nlines = FILE_LINES(files[i])
>
> data = STRARR(nlines)
>
> OPENR, lunit, files[i], /get_lun
>
> READF, lunit, data
>
> CLOSE, lunit
>
> FREE_LUN, lunit
>
>
>
> ; Do some stuff here
>
>
>
> endfor

This is great. Thank you very much.
Re: Open text files consecutively without knowing names [message #85267 is a reply to message #85266] Fri, 19 July 2013 13:10 Go to previous message
Phillip Bitzer is currently offline  Phillip Bitzer
Messages: 223
Registered: June 2006
Senior Member
Strictly speaking, you shouldn't need to close the lun if you use FREE_LUN:

http://www.exelisvis.com/docs/FREE_LUN.html

Just an FYI...
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: SVDC did not converge
Next Topic: Help with IDLnetURL

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:12:11 PDT 2025

Total time taken to generate the page: 0.00699 seconds