Open text files consecutively without knowing names [message #85264] |
Fri, 19 July 2013 09:22  |
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   |
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   |
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.
|
|
|
|