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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading file names from an external hard disk 1

Status
Not open for further replies.

Lloydone

Programmer
Jan 17, 2007
41
NZ
Can anyone give me a clue as to how to go about reading file names with a certain extention into an array via the read option.

Regards,
Lloyd
 
Code:
program read_filename

! Declare array of filenames with max length allowed by operating system
! that way the filenames may include the complete path ID

character*256 filename(10)
integer istat,j,nfiles 
nfiles=0

! open file where you read the filenames from

open(unit=20,file='inputfile.txt',status='old',iostat=istat)
rewind 20

! read filenames from file

if (istat.eq.0) then

  j=1
  do
    read(20,'(a256)',iostat=istat)filename(j)
    if(istat.ne.0)exit
    j=j+1
  enddo

! clear up

  close (unit = 20)
  nfiles = j

endif

! to open one of the files you might want to remove the trailing blanks
!(my compiler does not like trailing blanks in filenames)
! for instance using the intrinsic 'trim'
! If there may be leading blamks in your filenames use ADJUSTL first to lefttrim.

if(nfiles.gt.0)then
  open(unit=30,file=trim(filename(3)),........)

! enter your statements

endif

I never had any trouble with \ or other special characters in the filenames.



Norbert
 
xwb,

I am using an absoft compiler on a mac os x system.

Norbert,

I am using a similar code to read strings from a file. But can I open the external hard disk file , namely where you have inputfile.txt

can I have '/Volumes/disk/files' and then read the file names from only those files with the extension of '.hdf' ?

at the moment I just copy and past the file names into a text file i.e. 'names.txt' and then use the read operation from there.

Regards,

Lloyd
 
Don't know about absoft. I'm not sure but I think the underlying OS in Mac OSX is Unix. Check your library. Does it have opendir, readdir and closedir? If it does, you can use them to read the filenames from the directories.
 
Lloyd,

sorry, I do not understand what you mean. Could you just copy a few lines of your original readfile for explanation ?

Of course you can read a string and check if it contains a certain substring, your .hdf for instance. This is a Fortran intrinsic:

ipos = index(inputstring,'.hdf')

This would give the position of .hdf in the substring and 0 if the substring is not present. So if you check (integer) ipos for being greater than 0, you can decide on reading the next string or use it as a valid filename.

Is this what you are looking for ?

Norbert
 
So this is the same as the '(len=50)' command.

I will give it a shot.
But I was reading up on it and I believe the '/' is a special character as you said...
 
That last post was for the other thread, my bad.

Thanks for your last post Norbert this will help. Here is a copy of my 'readme' code :

!This Subroutine reads a file input as 'aString'
!then writes them into an array called holder


subroutine readme (aString)
use L2Parameters
implicit none
character(len=60) :: aString,extension
integer::n,ios

print*,'Opened readme'

extension='/Volumes/TB5/Albedo/2005/'
open (unit=9,file=aString,status='old',iostat=ios)
! print*, ios
rewind 9
n=0
do
n=n+1
if(n.gt.size(holder)) exit
read (9,*,iostat=ios) holder(n)
holder(n)=trim(extension)//trim(holder(n))
if(ios.ne.0) exit

enddo

close(9)

end subroutine readme


So I read in the file name , named 'aString' , then this opens it,reads the names into an array declared in my module named L2Parameters called holder.

The 'extension' concatenation has to do with my other post about '/' 's . Since the read operation may not read in these special charaters I have to manually include it as a string then add it to the string being read.


But what I want to happen it for my code to open up a given external hard disk and then read all the file names in that hard disk into an array. From there I can manipulate which files I need from that disk. I want to do this instead of copyingand pasting each individual file name I want from the hard disk and putting that into a text file which is then read like above. We are talking on the order of 150 to 300 files. A bit tedious.


You may ask why I do not copy and paste all files within the hard disk to a text file in one go instead of copying and pasting each individual NAME of the file, it is because my code does not seem to run if I do this...


Any thoughts.

Hope this is not to long and fully explains my situation.


Regards,

Lloyd
 
As mentioned earlier, if your system supports opendir, readdir etc, you can read all the names in the directory. Check


Look for opendir in that link. All you need to do is something like this (pseudocode)
Code:
call opendir (path, handle, errno)
do while (errno .eq. 0)
    call readdir (handle, name, namelen, errno)
    if (errno .ne. 0) exit
    ... do something with name, probably stat or fstat
end do
call closedir (handle, errno)
There was talk about adding opendir in 2003 so they might have added it by now unless you're using a very old compiler.
 
Lloyd,

I am still not so very sure if I got your problem right. Apparently I misunderstood one of your earlier posts as 'extension' usually indicates the three letter appendix to the filename indicateing its type like .doc or .xls.

What you got in your variable 'extension' I would call a path to the file that you want to read.

To have filenames and to combine them with some path information to get complete path+file identifyers that can be used by 'open' I would set about pretty much as you did.

So, what's the problem ?


Norbert
 
xwb,

thanks for your link I will see to it.


Norbert,

I am dealing with two types of files in my hard disk with the extensions '.hdf' and '.hdf.met' and I only want a code to read in file names with the '.hdf' extension.

In regards to my code mentioned above you are right that the variable should not be called 'extension' but rather 'pathname' or 'prefix'.

The problem lies in the fact that I want my code to open an external hard disk, read all file names into a text where I can then use 'open' and other such operations to deal with them.

At the moment my code does this manually, namely, I open up the hard disk and copy the NAMES of the files into a text, where I use 'readme' to read them off into an array. I want to make automated the step which has me manually copying and pasting into a text file.


Hope that explains it.

Regards,

Lloyd
 
I've used kludges like

call system( "DIR C:\directory >DIRFILE" )

to generate a file of the directory,

then read the directory file into the program.

It isn't pretty but it does work...
 
Lloyd,

I got now, that somewhere on your harddisk you have a directory which contains files with extension .hdf and .hdf.met and you want to process only the .hdf files. the path from your current working directory to the one where the files are is 'Volumes/TB5/Albedo/2005'.

I do not have a solution but I can tell you how I would set about:

(1) check the documentation of your compiler on routines that support file handling. There are to be some there since it is a problem that arises everywhere that you want to know about existent directories and files. In my system (Salford Fortran 95) there are quite a few routines that read complete directories into files, check existance of certain filenames, extract filenames that follow a certain pattern or whatever. Look for what might best suit you.

(2) Just meant as a hint: If it is in your hands to name the files that you want to work with, avoid the double extension .hdf.met, call the latter .hdm or whatever. Then you could check on existance of either .hdf or .hdm in your filename.

(3) Check on the routines supplied with your compiler that can handle strings, especially which one could check for the existence - or non-existence - of substrings in a string. These might not be instrinsics but I am sure Absoft supplied a couple of asuch routines with their library.

(4) In order to work your way through your problem first of all - I should have started with this one - clearify for yourself what the problem is that you want to solve. Proven by experience: if you cannot explain your problem to someone else - then the nature of the problem is not very clear to yourself. Take a piece of paper (old fashioned me) and write down:
- what have I got
- what do I want to get
- what is my idea on how to get from the first to the second
If you need more than say 10 words to describe where you are and 10 more words to define where you want to go - simplify. Example:

' I have files named .hgt mixed with .hgt.met in a directory called '...', but only the .hgts contain the ifo I need.'

this could be broken down to

- The path to my directory is ...
- I must get the filenames from there
- I must sort out the files of interest
- I can recognize these files by ...
- I must add my path information
- I must save the names in an array

For all of these steps my compiler has library functions that are not intrinsics but I am very sure, Absoft has such a library as well.


Lloyd, sorry , I got carried away writing this. I do not want to offend you by detailing in these basics, but it is often of great help to do such things. In fact,I do not write down my problems - my method is to explain my problems to my wife - who has very little knowledge on computers. If I can explain it to her so she understands it - just then I have the solution on how to solve my problem This takes more than 10 words each time - but is sure to lead me down to the basics of my problem.


Cheers
Norbert


 
I often use zeigenhost's solution too. Normally when it is a one off or I'm too lazy to do the opendir and stat bits. I think MACOSX is basically unix so the equivalent is

call system ('find . -name "*.hdf" -ctime 30 > fred.txt')

Then open fred.txt for the list of files
 
Norbert,
in actuality this simple breakdown has helped alot in the way I could conduct writting my code. So much so I have shown some of my colleagues :)

I am off to check the documentation of Absoft.


xwb,

could you explain your variables in call system.

Thanks Team

Lloyd
 
I assume you're talking about

find . -name "*.hdf" -ctime 30

find is a file search utility
. is the starting point i.e. the current directory
-name "*.hdf" is the template of the files you wish to find
-ctime 30 is for files older than 30 days

I'm not totally up on MacOSX. Do they have man pages like standard unix? If they do, just do a man on find. There are tons more options and some even stranger syntax.

I don't know if you can get to command prompts. If you type the find command as it stands, you will see what it outputs. This can be done with any command that is put into system.
 
Hi guys,
Could anyone help with a problem I am having with a Fortran code? I have written a code to open a file(with binary extension) and want to change this file into an integer format. Using a gfortran compiler on MAC OS, the program compiled well but on trying to execute it, I get this error message:

At line 11 of file convert2integer.f90
Fortran runtime error: End of file

Please, could anyone let me know what is wrong with this program? Here is the code:

PROGRAM convertToInteger

IMPLICIT NONE

REAL(KIND=4), DIMENSION(15,9,1) :: precipitation_real
INTEGER(KIND=4), DIMENSION(15,9) :: precipitation_int

OPEN(UNIT=10, FILE='upas_test.bin', FORM="unformatted", ACCESS="sequential")
OPEN(UNIT=11, FILE='upas_test_int.bin', FORM="unformatted", ACCESS="direct", RECL=15*9*4))

READ(UNIT=10) precipitation_real

precipitation_int = NINT((precipitation_real:),:,1)-273.16)*10.)

WRITE(*,'(15I5)') precipitation_int
WRITE(UNIT=11, REC=1) precipitation_int

CLOSE(UNIT=10)
CLOSE(UNIT=11)

END PROGRAM convertToInteger
 
Aclems - this is nothing to do with the original problem - you should start a new thread
 
Aclems - your 2nd OPEN line is too long. Try reducing to 80 chars and add a continuation char on the preceding line.
 
xwb-Thanks for your suggestion but the problem persists; it compiles but it won't execute. This is getting really frustrating.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top