From: Luka Djigas on
I need to read through a directory of files, that share no difference in their names ... uhmm,
they share some differences, but nothing that can be relied upon.

It is some measurement data for a base of models, with file names like
m1023.dat
4665.dat
4666-1.dat
and
184.dat

And then the variations ... m1024.dat, m1025.dat and so on ...

They all however share the same format, and I'm trying to read the first line from each file to
enable the user to see them all on screen before proceeding to the choice of model.

Is there a way to "go through" the directory and get all file names, in a loop ?
I don't know if that is even the right approach ... but cannot think of anything else right now.

-- Luka
From: Luka Djigas on
On Sun, 04 Apr 2010 03:40:10 +0200, Luka Djigas <ldigas@___gmail___.com> wrote:



Also, let not forget, merry Easter to all who it may concern :-))

Luka
From: Richard Maine on
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...

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.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
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 ?
>
>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
From: mecej4 on
On 4/3/2010 8:40 PM, Luka Djigas wrote:
> I need to read through a directory of files, that share no difference in their names ... uhmm,
> they share some differences, but nothing that can be relied upon.
>
> It is some measurement data for a base of models, with file names like
> m1023.dat
> 4665.dat
> 4666-1.dat
> and
> 184.dat
>
> And then the variations ... m1024.dat, m1025.dat and so on ...
>
> They all however share the same format, and I'm trying to read the first line from each file to
> enable the user to see them all on screen before proceeding to the choice of model.
>
> Is there a way to "go through" the directory and get all file names, in a loop ?
> I don't know if that is even the right approach ... but cannot think of anything else right now.
>
> -- Luka

For operating systems with file trees, there are a number of file-tree
"walking" routines available; most of them will be C-oriented, but can
be called from Fortran sometimes.

For Windows, for example, look up FindFirstFile and FindNextFile on
MSDN. Here is a short example from there:

#include <windows.h>
#include <stdio.h>

int main(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

if( argc != 2 ){
printf("Usage: %s [target_file_pattern]\n", argv[0]);
return;
}

printf ("Target file is %s\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf ("FindFirstFile failed (%d)\n", GetLastError());
return;
}
else
printf ("The first file found is %s\n",
FindFileData.cFileName);

while(FindNextFile(hFind,&FindFileData))
printf("The next file found is %s\n",FindFileData.cFileName);

printf("That's all\n");
FindClose(hFind);
}

--mecej4