Task

Walk a given directory and print the names of files matching a given pattern.

(How is "pattern" defined? substring match? DOS pattern? BASH pattern? ZSH pattern? Perl regular expression?)


Note: This task is for non-recursive methods.   These tasks should read a single directory, not an entire directory tree.

Note: Please be careful when running any code presented here.



Related task

   Walk Directory Tree   (read entire directory tree). 


/* NetRexx */
options replace format comments java crossref symbols nobinary
 
import java.util.List
 
runSample(arg)
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getFileNames(dirname, pattern) public static returns List
  dir = File(dirname)
  contents = dir.list()
  fileNames = ArrayList()
  loop fname over contents
    if fname.matches(pattern) then do
      fileNames.add(fname)
      end
    end fname
  Collections.sort(fileNames)
  return fileNames
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
  parse arg dirname pattern
  if dirname = '' then dirname = System.getProperty('user.dir')
  if pattern = '' then pattern = '^RW.*\\.nrx$'
 
  fileNames = getFileNames(dirname, pattern)
  say 'Search of' dirname 'for files matching pattern "'pattern'" found' fileNames.size() 'files.'
  loop fn = 0 while fn < fileNames.size()
    say (fn + 1).right(5)':' fileNames.get(fn)
    end fn
 
  return
 