Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read files directly from working folder?

Status
Not open for further replies.

mschoel

Technical User
Sep 19, 2007
3
US
I have a program that pulls input files from a working folder, runs calculations, and produces an output file. It then steps through all files in the folder. To recognize the input files, it reads in a text file containing every file name. I would rather not have to create this text file and have the program read in the input files directly, no matter their name. Is there a way in Fortran 90 to read file names directly and step through all the input files in my directory, without having to physically type in their names?

Here's an example of how it works now:

Program reads in LIST.TXT:
57
46
68
30
etc...

Assigns variable name to each line:
57 --> PSE(1)
46 --> PSE(2)
etc...

Program adds extensions to 57 and reads in input files:
57.IN
57.SPEC.DAT

Program spits out output file with same name:
57.OUT

Program steps on to next set of input files and adds extensions to 46:
46.IN
46.SPEC.DAT

Program spits out:
46.OUT

etc...

I would rather the program read directly from the working folder and step through every set of input files:
57.IN
57.SPEC.DAT
46.IN
46.SPEC.DAT
68.IN
68.SPEC.DAT
30.IN
30.SPEC.DAT

Is this possible?
 
I know it is Fortran 90 but which OS/Compiler are you using. Some of them have functions to change directories and step through the directory tree but it does depend on the compiler vendor.
 
Sorry...I'm relatively new to Fortran programming.

This is on Windows and Compaq Visual Fortran.
 
Sorry, I don't have CVF. Reading a directory is normally done in one of two ways:

a) The Unix way. This is not restricted to Unix but it is how they do it on Unix. There are 3 routines: opendir, readdir and closedir. opendir opens the directory with a particlar template, readdir reads the next entry. I think it returns 0 if it is OK and -1 on end of file. closedir, closes the directory.

b) The DOS way. There are 2 routines: findfirst and findnext. findfirst sets up the template and reads the first entry. findnext reads subsequent entries and returns NULL when there aren't anymore.

You may need to do a stat on each entry to find out whether it is a directory or not. Note that some systems will return . (i.e. the current directory) and .. (i.e. the parent directory) and you have to ignore them otherwise you'll recurse until the memory runs out.

Another way is to issue a system command to create the file which contains all the other files. Something like
Code:
call system ('dir /x/b ' // thedirectory // '> dirlist.txt')
! then open dirlist.txt for the list of files
 
Thank you, xwb, for your advice. Unfortunately my research into the topics you suggested was fruitless, but at least the code works with a text file listing the file names.

I appreciate your help!
 
hi I ofund this reference to Com Vis Fortran ver 6.6 would this help ?

File Management: table

Name Procedure Type Description

DELFILESQQ Run-time Function DELFILESQQ(files). Deletes the specified files in a specified directory.

FINDFILEQQ Run-time Function FINDFILEQQ(filename, varname, pathbuf). Searches for a file in the directories specified in the PATH environment variable.

FULLPATHQQ Run-time Function FULLPATHQQ(name, pathbuf). Returns the full path for a specified file or directory.

GETDRIVEDIRQQ Run-time Function GETDRIVEDIRQQ(drivedir).
Returns current drive and directory path.

GETFILEINFOQQ Run-time Function GETFILEINFOQQ(files, buffer, handle). Returns information about files with names that match a request string.

PACKTIMEQQ Run-time Subroutine PACKTIMEQQ(timedate, iyr, imon, iday, ihr, imin, isec). Packs time values for use by

SETFILETIMEQQ.

RENAMEFILEQQ Run-time Function RENAMEFILEQQ(oldname, newname). Renames a file.

SETFILEACCESSQQ Run-time Function SETFILEACCESSQQ(filename, access). Sets file-access mode for the specified file.

SETFILETIMEQQ Run-time Function SETFILETIMEQQ(filename, timedate). Sets modification time for a given file.
SPLITPATHQQ Run-time Function SPLITPATHQQ(path, drive, dir, name, ext). Breaks a full path into four components.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top