Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Opening multiple files???

Status
Not open for further replies.

mjs84

Aerospace
Aug 20, 2003
27
I am trying to open multiple files, 1 at a time, and pull data. I am not having any success. I can read the data from a file by hardcoding the file name, but I have been unable to read all the files in a directory until no more files exist. Can anyone advise me a path I should take??
 
Replies continue below

Recommended for you

The problem here appears to be that you may not be aware of the names of all the files in a directory. Is that correct?

You can open a file by hardcoding in the name or placing a name into a character variable. However.... the Fortran standard does not specify a way to read specific filenames on a given operating system. This is largely because every operating system is different. The concept of a file or a directory or a folder is not the same on all systems. In fact.... basic fortran does not know what a directory is. And of course, a file is defined specifically by the operating system you are working with. However, different compilers may provide you a means of obtaining such data ( though, these features are not standardized ). You'll have to read through your compilers documentation to learn how to do such things.

Alternatively... you can ( from within your fortran code ) run another external program that will give you the data you need. You could possibly have this other program write out file names to another temporary file. Then, of course, you could open up that file and read the names you want.

In the end... it all depends on which compiler you are using.

Dan :)
 
DanTex,
You are correct is assuming I don't know the names of the files. I guess I can force the downstream user to specify at least a standard file name with just integer increments at the end of the filename...but that might be limiting to them. I appreciate your help and all the help I have received from this site.

THANKS...
 
Alternative: Make a xxxx.bat file
dir name.ext>tmp.dir
program_name

The first line puts directory into tmp.dir, before running
progran_name.



<nbucska@pcperipherals.com>
 
NBucska's suggestion will definately work. The only reason I didn't suggest that one is because it has some hidden &quot;gotcha's&quot; that will get you if you aren't aware of them.

Microsoft ( with all it's power and budget ) is very inconsistent!!! In each version of Windows... it has the DIR command work slightly differently. The command that Nbucksa shows will create a data file. The problem is that the format in the data file won't necessarily be simple. Each version of Windows does it differently. Also, any individual can easily tweak their computer to alter the format to their liking. Then... there's the long name / short name stuff. Also... rarely ( if ever ) does the DIR command insert a period, '.' , between the main name and extension for a file.

Thus... you the programmer will have to go to some trouble to write code to parse through the ASCII file and decide what all the valid filenames are. Though this is very possible... it isn't necessarily easy and you will need logic to handle all of the different possible formats that this file may be written in. I've done this type of thing before for my &quot;quick and dirty&quot; work. But... I wouldn't want to do it for a client that expects things to work easily for them every time.

Dan :)
 
If you need to be more Win-version-independent, you could go the route of making Windows API calls to get the file names from the directory. Search the MSN web site for help on the various calls you need to make. A starting URL for you is:


If you haven't done any work with making API calls from Fortran, you may be able to find a package that contains the calls already packaged for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor