home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / getdir.exp < prev    next >
Text File  |  1990-07-25  |  4KB  |  100 lines

  1. # QTAwk utility to read directory for specified files
  2. #
  3. # file patterns are listed on the command line. Note that the syntax for
  4. # patterns follows the regular expression syntax and not the wildcard
  5. # syntax of DOS. For example the DOS wildcard expression for all files
  6. # ending in "exp" would be:
  7. #   "*.exp"
  8. # the regular expression syntax is:
  9. #   ".*\.exp"
  10. #
  11. # The regular expression syntax is a little more involved, but can
  12. # specify patterns that the DOS wildcard syntax cannot. For example, to find
  13. # all files starting with 'd', 'g' or 'h' and with an extension of
  14. # "hlp" or "txt", we would have the command line:
  15. #   QTAwk -fgetdir.exp "[gdh].*\.(hlp|txt)"
  16. # This is simply not possible with the DOS wildcard notation. Multiple
  17. # patterns may also be specified:
  18. #   QTAwk -fgetdir.exp "[gdh].*\.(hlp|txt)" ".*(dir|bnc)\.exp"
  19. #
  20. # The user function defined to find and print the desired filenames may be
  21. # extended to also include expressions for the file date/time/size to further
  22. # narrow down the files desired.
  23. #
  24. BEGIN {
  25.     local pattern;
  26.     local sub_dir = ""; # default to current sub-directory
  27.  
  28.     for ( i = 1 ; i < ARGC ; i++ ) {
  29.     pattern = '^' ∩ strupr(ARGV[i]) ∩ '$';
  30.     #
  31.     # the following two lines will convert the command line string
  32.     # into a regular expression. This is to prevent the constant
  33.     # conversion of a string into a regular expression in the
  34.     # "getdir" function. The following two lines could be deleted or
  35.     # commented out without degrading the performance too much
  36.     #
  37.     pattern = "pattern = /" ∩ pattern ∩ "/;";
  38.     execute(pattern);
  39.     #
  40.     # print pattern to look for as a double check, could be commented out
  41.     # when not necessary
  42.     #
  43.     print "Finding: " ∩ pattern;
  44.     #
  45.     # go find matching files and print names
  46.     #
  47.     getdir(sub_dir,pattern);
  48.     }
  49. }
  50.  
  51. # Function to get desired file names and print
  52. # Use DOS "dir" command to list all files to a temporary file
  53. # read temporary file picking up:
  54. # 1) path from third line
  55. # 2) filenames from remainder of lines
  56. # 3) match filenames against filepattern for desired files and print if match
  57. # Could also get file sizes, dates and times if desired
  58. # Possible extension:
  59. #  1) It would be possible to check each line for the pattern
  60. #  /<DIR>/ and find subdirectories. A tree traversal could then be done with
  61. #  complete regular expression control of the path through the tree.
  62. #  2) Add logic to check file size, date and/or time for matches against
  63. #  desired criteria. The criteria could be in a preset file or a file passed
  64. #  on the command line. The criteria could be expressed as arithmetic and/or
  65. #  logical equations. The equations could be executed with the "execute"
  66. #  function. The return value could then be checked for true/false and the
  67. #  file accepted/rejected accordingly.
  68. #
  69. # Arguments passed:
  70. #   dir ==> path of desired files
  71. #   file_template ==> string or regular expression pattern for filenames
  72. #
  73. function getdir(dir,file_template) {
  74.     local tmp_file = "$tmptmp$.dir";
  75.     local gdir = "dir " ∩ dir ∩ "*.* > " ∩ tmp_file;
  76.     local ddir = "del " ∩ tmp_file;
  77.     local tmparr, inline, filename;
  78.  
  79.     system(gdir);
  80.     fgetline(tmp_file,inline);    # read and discard header lines
  81.     fgetline(tmp_file,inline);    # read and discard header lines
  82.     fgetline(tmp_file,inline);    # read and discard header lines
  83.     split(inline,tmparr);    # split out to get path
  84.     path = tmparr[3] ∩ '\\';    # set path
  85. #    print path;         # un-comment to print path
  86.     fgetline(tmp_file,inline);    # read and discard header lines
  87.     while ( fgetline(tmp_file,inline) > 0 ) {
  88.       # discard if not a filename line
  89.     if ( inline !~ "^[A-Z]" ) continue;
  90.       # split out file information
  91.     nf = split(inline,tmparr);
  92.       # form "filename.ext"
  93.     filename = tmparr[1] ∩ '.' ∩ tmparr[2];
  94.     if ( filename ~~ file_template ) print filename;
  95.     }
  96.       # delete temporary directory file
  97.     close(tmp_file);
  98.     system(ddir);
  99. }
  100.