From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 25-AUG-1989 16:54
To:	MRGATE::"ARISIA::EVERHART"
Subj:	Re: How to search a directory for existance of files?

Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Fri, 25 AUG 89 12:57:36 PDT
Received: from ucbvax.Berkeley.EDU by KL.sri.com with TCP; Fri, 25 Aug 89 12:48:39 PDT
Received: by ucbvax.Berkeley.EDU (5.61/1.37)
	id AA18714; Fri, 25 Aug 89 12:25:29 -0700
Received: from USENET by ucbvax.Berkeley.EDU with netnews
	for info-vax@kl.sri.com (info-vax@kl.sri.com)
	(contact usenet@ucbvax.Berkeley.EDU if you have questions)
Date: 25 Aug 89 16:59:25 GMT
From: sun-barr!cs.utexas.edu!ut-emx!mike@ames.arc.nasa.gov  (John Howells)
Organization: Aeroflightdynamics Directorate
Subject: Re: How to search a directory for existance of files?
Message-Id: <2995@amelia.nas.nasa.gov>
Sender: info-vax-request@kl.sri.com
To: info-vax@kl.sri.com

In article <17583@ut-emx.UUCP>, mike@ut-emx.UUCP (Mike O'Donnell) writes...
< 
< I am interested in obtaining some sample source code to
< check a directory for the existence of certain files.  What
< I wish to do is look in one particular directory and see if
< any files match a VMS wildcard specification.  That is, I
< would like code that given the wildcard "*.LST;*" would 
< return (perhaps one-by-one) the list of files that match.
< Preferably the code is in C or Fortran.
< 
< Thanks, Mike O'Donnell

I tried to mail this, but it bounced so I'm posting it.

Here's a couple of FORTRAN examples. One is called SCAN and just does a
wildcard directory listing. The wildcard spec can be changed to suit your
purposes. The other is LS, a sort of Unix ls imitation. It takes foreign
strings to do a wildcard search (like 'ls *.exe' for instance). This is
probably more in line with what you're looking for, although not as simple
as the SCAN example. Hope this is of some help:

------------------------------< cut here >------------------------

        program scan
C
C wild card display of entire directory
C
        implicit integer*4 (a-z)
        integer*4 sys$setddir, len_disk
        character*30 root
        character*31 disk
        character*50 filename, outfile(1000), file(1000)

        filename = '*.*;*'

        i = 1

        call lib$sys_trnlog ( 'SYS$DISK', len_disk, disk )

        call sys$setddir ( 0, len_root, root )
        root = root(1:len_root)

        write (6,*) ' '
        write (6,*) 'Directory ', disk(:index(disk,' ')-1),root
        write (6,*) ' '

        do while (sts.eq.rms$_normal)
           istat = lib$find_file ( filename, outfile(i),
     .      icontext ,,, sts )
           if (.not.istat) go to 10
           write (6,*) outfile(i)
           i = i + 1
        enddo

10      i = i - 1
        if (i.ne.0) then
           write (6,*) ' '
           if (i.eq.1) then
              write (6,200) 'Total of', i, ' file.'
           else
              write (6,200) 'Total of', i, ' files.'
           endif
        else
           write (6,*) '%DIRECT-W-NOFILES, no files found'
        endif

200     format (1x,a,i4,a)

        end

-------------------------< cut here >-----------------------

        program ls
C
C This program is merely a toy to demonstrate the LIB$FIND_FILE routine. The
C purpose of this program is to scan the directory for the specified files and
C translate the filespecs to lowercase, trim the version numbers, suppress
C printing of multiple versions, and display in three column format ala Unix.
C
C Author: John Howells - Sterling Software     3-1-88
C
        implicit integer*4 (a-z)
        character*80 filename, outfile(1000), file(1000)
        character*30 log_tran
C
C check for the presense of a foreign command line.
C
        call lib$get_foreign ( filename ,, filename_len )
        if (filename_len.gt.0) then
           istat = lib$sys_trnlog ( filename(1:filename_len),
     .      log_len, log_tran )
           if (istat.eq.1) then
              filename = log_tran(1:log_len)//'*.*;*'
           else
              bracket = index(filename,']')
              star = index(filename,'*')
              period = index(filename,'.')
              if (bracket.eq.filename_len) then
                 if (star.eq.0.and.period.eq.0) then
                    filename = filename(1:bracket)//'*.*;*'
                 endif
              elseif (bracket.ne.filename_len.and.star.eq.0.and.
     .         period.eq.0) then
                 filename = filename(1:filename_len)//'.*;*'
              elseif (star.gt.0.and.period.eq.0) then
                 filename = filename(1:filename_len)//'.*;*'
              elseif (period.eq.1) then
                 filename = '*'//filename(1:filename_len)
              endif
           endif
        elseif (filename_len.eq.0) then
           filename = '*.*;*'
        endif
        i = 1
        j = 1
C
C loop through the directory, picking out the appropriate files.
C Strip the ;version from the file. Ignore multiple versions of
C files. Write the resuling files to the array File(1000).
C
        do while (sts.eq.rms$_normal)
           istat = lib$find_file ( filename, outfile(i),
     .      icontext ,,, sts )
           if (.not.istat) go to 10
           bracket = index(outfile(i),']') + 1
           semicolon = index(outfile(i),';') - 1
           outfile(i) = outfile(i)(bracket:semicolon)
           file_end = index(outfile(i),' ') - 1
           istat = lowercase(outfile(i)(1:file_end))
           if (outfile(i).ne.outfile(i-1)) then
              file(j) = outfile(i)
              j = j + 1
           endif
           i = i + 1
           k = k + 1
        enddo
C
C loop through the resulting array, File(1000), and print the filenames
C in three column format (ala Unix)
C
10      if (k.eq.0) then
           istat = lowercase ( filename(1:filename_len) )
           write (6,*) filename(1:filename_len),' not found'
           call exit
        endif
        do x = 1,j-1,3
           write (6,200) file(x),file(x+1),file(x+2)
        enddo

200     format (1x,a26,a26,a26)

        end

C************************************************************************

        integer function lowercase ( string )
C
C This function converts uppercase to lowercase
C
        implicit integer*4 (a-z)
        character*(*) string
        character*1  character

        length = len(string)

        do x = 1,length
           character = string(x:x)
           if (ichar(character) .ge. ichar('A') .and.
     .      ichar(character) .le. ichar('Z')) then
              charint = ichar(character) + ichar('a') - ichar('A')
              string(x:x) = char(charint)
           elseif (ichar(character) .eq. 119) then
              string(x:x) = ' '
           endif
        enddo

        end

--------------------------------------------------------------------------------
John Howells           |  "Science does not    | howells@pioneer.arc.nasa.gov
Sterling Software      |   remove the terror   | howells@krypton.arc.nasa.gov
Palo Alto, Ca.         |   of the Gods"        | howells%kry@ames.arc.nasa.gov

