From: Luka Djigas on
On Sat, 3 Apr 2010 19:14:56 -0700, nospam(a)see.signature (Richard Maine) wrote:

>Luka Djigas <ldigas@___gmail___.com> wrote:
>
>> Is there a way to "go through" the directory and get all file names, in a
>loop ?

I forgot to mention one thing ... I don't know if it matters, but the program is in the directory
in which the files need to be processed are.

So he's not walking through a tree of dir/subdirectories.

I guess it doesn't make that much of a difference, but still, to avoid misunderstandings.

-- Luka
From: Sjouke Burry on
Luka Djigas wrote:
> On Sat, 3 Apr 2010 19:14:56 -0700, nospam(a)see.signature (Richard Maine) wrote:
>
>> Luka Djigas <ldigas@___gmail___.com> wrote:
>>
>>> Is there a way to "go through" the directory and get all file names, in a
>> loop ?
>>
>> Standard Fortran doesn't even have a concept of a directory, so no,
>> there is nothing portable.
>>
>> Depending on the particular OS and compiler, there might be vendor
>> routines for that, but as you didn't mention a particular OS or
>> compiler...
>
> Mea culpa. And what's worse, after all those "inquire the existence of
> a directory" discussions :)
> The compilers in question are either gfortran or Intel's, running on Windows.
>
>> One approach is to spawn a shell command to list the file names,
>> redirect the output of that shell command to a file, and then read that
>> file. Of course, the exact shell command to use depends on the OS, and
>> the exact way to spawn a shell command depends on the compiler vendor.
>> So the details are non-portable, but the general idea ports well and can
>> be isolated.
>
> Yes, that's one approach. But the idea of reading the output of a DIR command (for example, although
> I could easily use LS from unix tools as well) seem like a good way to terribly complicate your life
> (all that string handling).
>
> Do you perhaps have any advices on the handling of that output ?
>
> I'm thinking it may be easier to maybe redirect the output of a DIR command to a file, then post
> process it through some regex (just a thought), and then read that file in a fortran program.
> This is as close to portable, as the moon is to an ant, but if I'm handling it "dirty" I might as
> well keep as much of it from the program.
>
>
> -- Luka
Have you ever tried: dir /b /a-d [yourdirectorylocation] ??
Just a clean list of filenames.
From: Richard Maine on
Sjouke Burry <burrynulnulfour(a)ppllaanneett.nnll> wrote:

> Luka Djigas wrote:
> > On Sat, 3 Apr 2010 19:14:56 -0700, nospam(a)see.signature (Richard Maine)
wrote:

> >> One approach is to spawn a shell command to list the file names,
> >> redirect the output of that shell command to a file, and then read that
> >> file.

> > Yes, that's one approach. But the idea of reading the output of a DIR
> > command (for example, although I could easily use LS from unix tools as
> > well) seem like a good way to terribly complicate your life (all that
> > string handling).
> >
> > Do you perhaps have any advices on the handling of that output ?

> Have you ever tried: dir /b /a-d [yourdirectorylocation] ??
> Just a clean list of filenames.

Yeah. I don't know the Windows switches for DIR (even when I'm on
Windows, I usually use JPSoft's 4NT), but there is bound to be a set
that makes it pretty easy. I'm not sure what "all that string handling"
you are referring to would be. Picking out blank-delimitted fields is
pretty easy (and there is probably a set of switches where you don't
even need to do that, but as Sjouke said, has just the filenames).

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: jwm on
On Apr 3, 8:25 pm, Luka Djigas <ldigas@___gmail___.com> wrote:
> On Sat, 3 Apr 2010 19:14:56 -0700, nos...(a)see.signature (Richard Maine) wrote:
> >Luka Djigas <ldigas@___gmail___.com> wrote:
>
> >> Is there a way to "go through" the directory and get all file names, in a
> >loop ?
>
> >Standard Fortran doesn't even have a concept of a directory, so no,
> >there is nothing portable.
>
> >Depending on the particular OS and compiler, there might be vendor
> >routines for that, but as you didn't mention a particular OS or
> >compiler...
>
> Mea culpa. And what's worse, after all those "inquire the existence of
> a directory" discussions :)
> The compilers in question are either gfortran or Intel's, running on Windows.
>
>
>
> >One approach is to spawn a shell command to list the file names,
> >redirect the output of that shell command to a file, and then read that
> >file. Of course, the exact shell command to use depends on the OS, and
> >the exact way to spawn a shell command depends on the compiler vendor.
> >So the details are non-portable, but the general idea ports well and can
> >be isolated.
>
> Yes, that's one approach. But the idea of reading the output of a DIR command (for example, although
> I could easily use LS from unix tools as well) seem like a good way to terribly complicate your life
> (all that string handling).
>
> Do you perhaps have any advices on the handling of that output ?
>
> I'm thinking it may be easier to maybe redirect the output of a DIR command to a file, then post
> process it through some regex (just a thought), and then read that file in a fortran program.
> This is as close to portable, as the moon is to an ant, but if I'm handling it "dirty" I might as
> well keep as much of it from the program.
>
> -- Luka

With Intel (Visual) Fortran, you have at least four choices:
*Use SYSTEM/SYSTEMQQ to invoke a shell command, redirected to some
file (those functions are included in the IFPORT module)
*Use GETFILEINFOQQ. The function is also included in the IFPORT
module; you might want to take a look at the documentation, to know
how to use the FILE$FIRST, FILE$LAST and FILE$ERROR handles.
*Use the POSIX subroutines PXFOPENDIR/PXFREADDIR/PXFCLOSEDIR (included
in the IFPOSIX module)
*If on Windows, use the FindFirstFile/FindNextFile functions (which
are part of the kernel32.dll; their interfaces are included in the
KERNEL32 module)

I don't know if gfortran offers something else besides the SYSTEM
function and subroutine.
From: Jason Blevins on
On 2010-04-04, Luka Djigas <ldigas@___gmail___.com> wrote:
> Is there a way to "go through" the directory and get all file names,
> in a loop ?

One option, which seems to me to be more portable than using
system calls, is to use POSIX routines. This would of course
require a POSIX library on Windows, DOS, etc. such as Cygwin,
but should work very well on Unix-like systems such as Linux,
BSDs, Solaris, OS X, etc.

Posix90 provides a very complete Fortran 90 POSIX interface:

http://savannah.nongnu.org/projects/posix90/

Unfortunately the kind parameters in this interface are defined
in a non-portable way (across compilers) through setting the kind
numbers directly, rather than using the kind inquiry intrinsics,
but I would guess that it should work as is in most cases.

The documentation will be built automatically upon running
make, which builds the library and produces and HTML and PDF
documentation in the /doc subdirectory.

The routines of interest to your case are in the f90_unix_dirent
module (opendir, closedir, readdir, etc.). Here is a simple
example:

program ls
use f90_unix_dirent
implicit none

type(DIR) :: dirp
integer :: errno, name_len
character(LEN=128) :: name

call opendir('/home/jrblevin', dirp)
do
call readdir(dirp, name, name_len)
if (name_len > 0) then
print *, name(1:name_len)
else
exit
end if
end do
call closedir(dirp)
end program ls

To compile and link it (for example):

$ export POSIX90=$HOME/src/posix90/src
$ gfortran --std=f95 -I$POSIX90 -o ls ls.f90 -L$POSIX90 -lposix90

I've added this information to the wiki, feel free to edit or
expand it:

http://fortranwiki.org/fortran/show/Posix90

There is another similar library called fortranposix, which seems
less mature and less well-documented than Posix90, but might still
be useful:

http://sourceforge.net/projects/fortranposix/

--
Jason Blevins
Ph.D. Candidate, Department of Economics, Duke University
http://jblevins.org/