- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am a new Fortran user with a question about file handling. I need to write a program to look in a particular directory, find all files with extension *.asc, open the files one at a time, and then perform read operations and data analysis. I have no problem opening a single file but I would like to know how to get the names of the files in a directory. Can anyone give me a suggestion?
Thanks,
Bob
I am a new Fortran user with a question about file handling. I need to write a program to look in a particular directory, find all files with extension *.asc, open the files one at a time, and then perform read operations and data analysis. I have no problem opening a single file but I would like to know how to get the names of the files in a directory. Can anyone give me a suggestion?
Thanks,
Bob
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Windows API calls may be made directly from Fortran. There are examples in the docs. You could write Windows specific code which works with multiple Fortran compilers.
ifort also supports functions such as the PXF functions which perform directory operations. Those are described in the library documentation. This way, you may be able to work with ifort on both linux and Windows.
Alternatively, you could use SYSTEM function calls to employ shell commands (Windows CMD shell, SFU shell, cygwin shell, ...) e.g.
i=system("dir *.asc > dirlst.txt 2>&1")
OPEN dirlst.txt, READ file names, OPEN them,...
ifort also supports functions such as the PXF functions which perform directory operations. Those are described in the library documentation. This way, you may be able to work with ifort on both linux and Windows.
Alternatively, you could use SYSTEM function calls to employ shell commands (Windows CMD shell, SFU shell, cygwin shell, ...) e.g.
i=system("dir *.asc > dirlst.txt 2>&1")
OPEN dirlst.txt, READ file names, OPEN them,...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would recommend using the Win32 API for this, or the library function GETFILEINFOQQ, which you can read about in the on-disk documentation. Executing a directory command and reading the output is not necessary.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tim18,
So would you say the system function option is the most robust? I don't want to write Windows specific code.
I inserted the code into my program and dirlst.txt outputs the following (for 2 files).
---------------------------------------------------------------------------------------------------------------
Volume in drive C has no label.
Volume Serial Number is ...
Directory of C:Documents and Settings....
05/29/2008 10:16 AM 15,067,555 OWI Sample Data.asc
06/04/2008 02:48 PM 555,837 OWI Sample Data2.asc
2 File(s) 15,623,392 bytes
0 Dir(s) 53,729,853,440 bytes free
-------------------------------------------------------------------------------------------------------------------
Will the filenames always start on the 6th line and the number of files always on the second to last line?
Thanks,
Bob
So would you say the system function option is the most robust? I don't want to write Windows specific code.
I inserted the code into my program and dirlst.txt outputs the following (for 2 files).
---------------------------------------------------------------------------------------------------------------
Volume in drive C has no label.
Volume Serial Number is ...
Directory of C:Documents and Settings....
05/29/2008 10:16 AM 15,067,555 OWI Sample Data.asc
06/04/2008 02:48 PM 555,837 OWI Sample Data2.asc
2 File(s) 15,623,392 bytes
0 Dir(s) 53,729,853,440 bytes free
-------------------------------------------------------------------------------------------------------------------
Will the filenames always start on the 6th line and the number of files always on the second to last line?
Thanks,
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, I wouldn't say the system function is most robust, although it may involve fewer changes if you support a wide range of operating systems. I suppose you could assume on Windows there would always be a Directory line, with the file names starting on the next non-blank line. Note Steve's recommendation of the Windows API if you wish to support only Windows.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The use of "system" and "dir" is much less portable than using GETFILEINFOQQ, which Intel Fortran supports on all platforms. If you must use "dir", explore the various options to produce a more usable listing, such as /B.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am going to use the GETFILEINFOQQ function. Thank you Steve and Tim for the help!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page