Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

file names

Bob_Fratantonio
Beginner
777 Views
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
0 Kudos
6 Replies
TimP
Honored Contributor III
777 Views
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,...
0 Kudos
Steven_L_Intel1
Employee
777 Views
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.
0 Kudos
Bob_Fratantonio
Beginner
777 Views
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
0 Kudos
TimP
Honored Contributor III
777 Views
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.
0 Kudos
Steven_L_Intel1
Employee
777 Views
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.
0 Kudos
Bob_Fratantonio
Beginner
777 Views
I am going to use the GETFILEINFOQQ function. Thank you Steve and Tim for the help!
0 Kudos
Reply