f



how do I read all the files in a given directory one by one?

I want to read one file in a directory, then the next file in the same
directory, then the next file, until the last file in the directory. I
don't know how many files are in the directory. Can anybody teach me
how to accomplish this task?

Do I have to use a system call dir and store the file names in another
file and then access all the files? Or is there a shortcut to do this?

Thank you

William
0
huxiankui (25)
6/18/2008 4:10:38 PM
comp.lang.fortran 11941 articles. 2 followers. Post Follow

14 Replies
1491 Views

Similar Articles

[PageSpeed] 0

On Wed, 18 Jun 2008 09:10:38 -0700 (PDT), william <huxiankui@gmail.com> wrote:

>I want to read one file in a directory, then the next file in the same
>directory, then the next file, until the last file in the directory. I
>don't know how many files are in the directory. Can anybody teach me
>how to accomplish this task?
>
>Do I have to use a system call dir and store the file names in another
>file and then access all the files? Or is there a shortcut to do this?

Which compiler are you using?  If it's CVF or Intel Fortran (on
Windows/Linux/MacOS), you can use the library routine GETFILEINFOQQ to do
this.  For other compilers there may be other methods. There is no Fortran
standard method available.

--
Steve Lionel
Developer Products Division
Intel Corporation
Nashua, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
  http://softwareforums.intel.com/
Intel Fortran Support
  http://support.intel.com/support/performancetools/fortran
My Fortran blog
  http://www.intel.com/software/drfortran
0
6/18/2008 4:27:22 PM
On Jun 18, 11:27=A0am, Steve Lionel <Steve.Lio...@intel.invalid> wrote:
> On Wed, 18 Jun 2008 09:10:38 -0700 (PDT), william <huxian...@gmail.com> w=
rote:
> >I want to read one file in a directory, then the next file in the same
> >directory, then the next file, until the last file in the directory. I
> >don't know how many files are in the directory. Can anybody teach me
> >how to accomplish this task?
>
> >Do I have to use a system call dir and store the file names in another
> >file and then access all the files? Or is there a shortcut to do this?
>
> Which compiler are you using? =A0If it's CVF or Intel Fortran (on
> Windows/Linux/MacOS), you can use the library routine GETFILEINFOQQ to do
> this. =A0For other compilers there may be other methods. There is no Fort=
ran
> standard method available.
>
> --
> Steve Lionel
> Developer Products Division
> Intel Corporation
> Nashua, NH
>
> For email address, replace "invalid" with "com"
>
> User communities for Intel Software Development Products
> =A0http://softwareforums.intel.com/
> Intel Fortran Support
> =A0http://support.intel.com/support/performancetools/fortran
> My Fortran blog
> =A0http://www.intel.com/software/drfortran

Thank you, Steve.
I'm using Lahey Fortran. Have you used Lahey before? Maybe I need to
ask the Lahey reps as to how to solve the problem.

William
0
huxiankui (25)
6/18/2008 6:21:56 PM
On Wed, 18 Jun 2008 11:21:56 -0700 (PDT), william <huxiankui@gmail.com> wrote:

>I'm using Lahey Fortran. Have you used Lahey before? Maybe I need to
>ask the Lahey reps as to how to solve the problem.

Asking Lahey, or perhaps first looking in the Lahey documentation, would seem
to be appropriate.  If I recall correctly, Lahey has their own user forum
which would be a good place to ask.
--
Steve Lionel
Developer Products Division
Intel Corporation
Nashua, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
  http://softwareforums.intel.com/
Intel Fortran Support
  http://support.intel.com/support/performancetools/fortran
My Fortran blog
  http://www.intel.com/software/drfortran
0
6/18/2008 6:35:50 PM
On Wed, 18 Jun 2008 09:10:38 -0700 (PDT), william
<huxiankui@gmail.com> wrote:

>I want to read one file in a directory, then the next file in the same
>directory, then the next file, until the last file in the directory. I
>don't know how many files are in the directory. Can anybody teach me
>how to accomplish this task?

Do the files have some mutual characteristic ?
for example file001, file002 ... in which case inquiry-ing can help ?

pozdrav
digas

>
>Do I have to use a system call dir and store the file names in another
>file and then access all the files? Or is there a shortcut to do this?
>
>Thank you
>
>William
0
Luka
6/18/2008 10:00:24 PM
What you can do, is use the SYSTEM call as below, to ask for a
directory list to a temporary file.
You can request this list in one of several orders, by storage,
alphabetically, date, type and so on, by selection from the standard
MS options. You can also ask for a short-width format report to leave
just name and dates and make the file be of shorter, simple format
ascii records.

 CALL SYSTEM('DIR \mydirectory /B /OD /options > \anotherdirectory
\tempfile')

This example asks by date /OD), with no header information (/B) so all
the list is just file names.

Then you open the temporary file as an input formatted text file and
process the directory list one by one, getting at eache filename and
using it.
This works in CVF, DVF, MS, Lahey and more.

You can ALSO do this using API's and nonstandard routine service calls
supplied by some vendors, as Steve Lionel suggests.
0
tbwright (1100)
6/18/2008 11:49:53 PM
william wrote:

> I want to read one file in a directory, then the next file in the same
> directory, then the next file, until the last file in the directory. I
> don't know how many files are in the directory. Can anybody teach me
> how to accomplish this task?

> Do I have to use a system call dir and store the file names in another
> file and then access all the files? Or is there a shortcut to do this?

Yes, that is probably the best way, though not so portable.

Where do you write the temporary file with the names?
What command do you use to generate it?
(Different for DOS/Windows and unix and (other OS)).

If you write it to the current directory, you will probably
want to exclude it from the list.  Be sure to use a name
that won't conflict with existing files, or other programs
doing the same thing.  More usual would be to write to a
temporary directory, such as the specified by the TEMP
environment variable (windows) or TMPDIR (Unix).

-- glen

0
gah (12851)
6/19/2008 12:20:31 AM
Terence <tbwright@cantv.net> wrote:

> What you can do, is use the SYSTEM call as below, to ask for a
> directory list to a temporary file.

Yes, that's exactly what Glen was talking about. Unfortunately, this
doesn't answer any of the problems Glen mentioned.

> This works in CVF, DVF, MS, Lahey and more.

And it doesn't work on any OS other than DOS/Windows, which was one of
the issues Glen mentioned. Also this did nothing to address the issues
that Glen mentioned of potential name conflicts and of what directory to
use for the temporary file. I suppose that, even though it was posted as
a followup to Glen's post, it wasn't actually intended to have anything
to do with what it was following up to.

Even with all its potential problems, it is often the best solution.
I've done it myself. I've most commonly done it on Unix systems, but the
ideas are the same, which is part of the advantage; although you can't
use the exact same code, you can use the same general structure. You can
probably keep the needed changes down to a line or two, which is a lot
less than the hassle of porting system API calls.

-- 
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain
0
nospam47 (9747)
6/19/2008 12:41:48 AM
Hi,

I designed the m_vfile module of the flibs project by Arjen Markus
exactly to (try to) solve all the problems mentioned here,
that is to say :

- stay independent from the OS,
- stay independent from the compiler,
- give access to files and directories,
- manage dynamic strings so that the files have names
computed dynamically with unlimited size,
- manage string lists, so that one can get a list of files,

that's why I cannot resist to try it here.
Here is a sketch of what would be possible (but untested) :

use m_vfile, only : vfile_listfiles
use m_vstring, only : t_vstring
use m_vstringlist , only : t_vstringlist, vstrlist_length,
vstrlist_index
type ( t_vstringlist ) :: listOfFiles
integer :: nbfiles
integer :: ifile
type ( t_vstring ) :: filename
integer :: lunit
listOfFiles =3D vfile_listfiles ( "directory" )
nbfiles =3D vstrlist_length ( listOfFiles )
do ifile =3D 1 , nbfiles
   filename=3D vstrlist_index ( listOfFiles , ifile )
   lunit =3D vfile_open ( filename , action=3D'read',status=3D'old',
iostat=3Dlocal_status )
   read(lunit, ....
   close(lunit)
   call vstring_free ( filename)
enddo
call vstrlist_free ( listOfFiles )

The internal methods to do that are "OS+compiler" dependent.
Internally, the vfile_listfiles method is based on a compiler-
dependent
"system" (function or subroutine) call to a system-dependent
command : ls on Unix, dir on win32.
A temporary dynamic temporary file name is generated in the system-
dependent
temporary directory with a compiler-dependent command to access
to a system-dependent environment variable.
All these dependencies are managed at compile-time so that the
client code (like the above one) is kept independent from all
these nasty details.

But that experimental solution may be less powerful than
compiler-specific commands, or even less powerful than what can be
achieved in another language, like, say Java, Tcl, C++, etc...

Micha=EBl
0
6/19/2008 9:49:20 AM
Re Richard's comment.
When one type a posting at point A, and eventually hits "Send", one's
posting may be a few seconds later than one or more other postings and
so NOT follow point A at all, but after one or more also-new postings.
So the response order is at best approximate.

And there is a call for assigning a random non-duplicate name as a
temporary file, and I believe, an OPEN parameter which treats the file
and non-keepable by default.
Even so, I would invent my own on the same directory (since it's my
machine) and ignore it and remove it if doing the poster's operation.
0
tbwright (1100)
6/19/2008 11:07:43 AM
Terence <tbwright@cantv.net> wrote:

> When one type a posting at point A, and eventually hits "Send", one's
> posting may be a few seconds later than one or more other postings and
> so NOT follow point A at all, but after one or more also-new postings.
> So the response order is at best approximate.

No. Response order has nothing to do with what you are following up to.
The post you are following up to is specifically identified when you
compose the post and does not change just because something else was
posted subsequently. You can follow up to any post - not just the last
one in the thread.

"Follow-up" does not mean just "the next posting in the thread". Rather
it means specifically identified as being in reply to what you are
following up. It is reasonably common for the conversation in a thread
to drift and split into 2 or more quite unrelated subjects. The followup
mechanism keeps those separate subthreads clearly separate.

Usenet was designed when response times were measured in days - multiple
ones - not seconds or minutes. Its design accomodates that just fine.
Even today, it is not particularly rare for someone to have been offline
for a while and jump in to respond to a post from multiple weeks before.
That only works because the system does indentify exactly what they are
responding to.

Of course, as with any medium, the writer needs to understand enough
about the medium to write appropriately for it. That includes such
things as following up to the right post and quoting sufficiently to
provide context so that the reader isn't expected to just intuit that
this responds to some paragraph from a week ago that the reader might
not even have read before.

-- 
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain
0
nospam47 (9747)
6/19/2008 4:20:39 PM
Thank you all very much. I tried a different approach. That is, to
general the directory structure using non-Fortran programs and then
feed the file into Fortran again. It's a little clumpsy, though.

William
0
huxiankui (25)
6/19/2008 10:39:49 PM
I've also tried Terence's solution and it works perfectly for me.

Here is the code:

   dirname='DIR /w /b '//'m:\FilesD\'//readdate//' > I:\BillHu\Proj
\files.txt'
   CALL SYSTEM(dirname)

Thank you all for the great suggestions and detailed solutions!

William
0
huxiankui (25)
6/20/2008 12:05:50 AM
I've also tried Terence's solution and it works perfectly for me.

Here is the code:

   dirname='DIR /w /b '//'m:\FilesD\'//readdate//' > I:\BillHu\Proj
\files.txt'
   CALL SYSTEM(dirname)

Thank you all for the great suggestions and detailed solutions!

William
0
huxiankui (25)
6/20/2008 12:06:23 AM
Ok, sorry, I should have directly reply to the 1st post,
since my message was more an answer to the original question,
than an answer to the other replies.
I was fooled by the google interface, which does not shows the thread
tree,
but simply display messages one by one. I should may be switch
to a "thread" view, but I do not see it.
0
6/20/2008 8:35:14 AM
Reply: