home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / CLIPPER / DBFDIR / DBFDIR.PRG
Text File  |  1993-11-26  |  4KB  |  142 lines

  1. // DBFDir
  2. //
  3. // Author : ShaunB..  (Shaun Botha [CI$ 70043,2641])
  4. // Purpose: Emulate DOS's DIR command but with special handling for DBF files -
  5. //          show lastRec() and fcount() for each DBF file
  6. // Compile: Clipper DBFDIR /n/m/w
  7. // Link   : Blinker / RTLink fi DBFDIR
  8. // Notes  : This is 'fame-ware' - copy as much as you want, modify all you want,
  9. //          distribute all you want.  Just include my name as the original
  10. //          author.
  11.  
  12. #include "directry.ch"
  13.  
  14. procedure main(cSpec, cSwitch)
  15.     local aFiles, x
  16.     local cBase, cExt, cSize, cDate, cTime, nTotSize
  17.     local dLUpd, nRecs, lPaged, nRows, lAborted
  18.  
  19.     // Fix our parameters
  20.     if cSpec != nil
  21.         cSpec := upper(cSpec)
  22.     endif
  23.     if cSwitch != nil
  24.         cSwitch := upper(cSwitch)
  25.     else
  26.         cSwitch := ""
  27.     endif
  28.  
  29.     // Determine whether display is to be paged
  30.     lPaged := .f.
  31.     if cSpec == "/P"
  32.         lPaged := .t.
  33.         cSpec := ""
  34.         cSwitch := ""
  35.     endif
  36.     if cSwitch == "/P"
  37.         lPaged := .t.
  38.     endif
  39.  
  40.     // Default to "*.*"
  41.     if empty(cSpec)
  42.         cSpec := "*.*"
  43.     endif
  44.  
  45.     // Just some blurb
  46.     ? "DBFDIR v1.0 (c) 1993 ShaunB.."
  47.     ?
  48.  
  49.     if cSpec == "/?"
  50.         ? "Displays a directory similar to that of DOS's DIR command.  In addition"
  51.         ? "the program will display the number of records and fields in a dBase file."
  52.         ?
  53.         ? "Note: This program does not support path names in DBF files (yet)."
  54.         ?
  55.         ? "usage: DBFDIR [file_spec] [/P]"
  56.         ?
  57.         quit
  58.     endif
  59.  
  60.     ?
  61.  
  62.  
  63.     lAborted := .f.
  64.     errorBlock({|e| doBreak(e) })                // Post a basic error handler
  65.     aFiles := directory(cSpec, "HSD")      // Get all files
  66.     nRows    := 0                          // Initialize row count for pages
  67.  
  68.     // Figure total size of files
  69.     nTotSize := 0
  70.     aeval(aFiles, {|f| nTotSize += f[F_SIZE] })
  71.  
  72.     for x := 1 to len(aFiles)
  73.         if inkey() == 27
  74.             lAborted := .t.
  75.             exit
  76.         endif
  77.  
  78.         nRows++
  79.         cBase := cExt := ""                 // Init these vars
  80.  
  81.         // Treat directories differently to 'normal' files
  82.         if at("D", aFiles[x, F_ATTR]) != 0
  83.             cSize := padr("<DIR>", 8)
  84.             cBase := aFiles[x, F_NAME]
  85.         else
  86.             if at(".", aFiles[x, F_NAME]) != 0
  87.                 cBase := left(aFiles[x, F_NAME], at(".", aFiles[x, F_NAME])-1)
  88.                 cExt  := substr(aFiles[x, F_NAME], at(".", aFiles[x, F_NAME])+1)
  89.             else
  90.                 cBase := aFiles[x, F_NAME]
  91.             endif
  92.             cSize := ltrim(str(aFiles[x, F_SIZE]))
  93.             cSize := padl(cSize, 8)
  94.         endif
  95.  
  96.         // Get date and time - convert time to look like DOS
  97.         cDate := dtoc(aFiles[x, F_DATE])
  98.         cTime := left(aFiles[x, F_TIME], 5) + iif(val(left(aFiles[x, F_TIME], 2)) < 12, "a", "p")
  99.  
  100.         // Show the info
  101.         if lPaged
  102.             if nRows > maxRow()
  103.                 ?? "Press any key to continue or [Esc] to quit"
  104.                 if inkey(0) == 27
  105.                     lAborted := .t.
  106.                     ?
  107.                     exit
  108.                 endif
  109.                 ?
  110.                 nRows := 0
  111.             endif
  112.         endif
  113.         ?? padr(cBase, 9), padr(cExt, 3), cSize, cDate, cTime
  114.  
  115.         // If this is a DBF file then show some extra info
  116.         if at(".DBF", aFiles[x, F_NAME]) != 0
  117.             begin sequence
  118.                 use (aFiles[x, F_NAME]) new shared
  119.                 ?? "..." + padl(ltrim(str(lastRec())),8,".") + " Records    " + padl(ltrim(str(fcount())),4)+" Fields"
  120.                 use
  121.             recover
  122.                 // More than likely caused by the current directory not being the
  123.                 // one where the DBF is located
  124.                 ?? "..Could not open file"
  125.             end sequence
  126.         endif
  127.         ?
  128.     next x
  129.  
  130.     // Show some summary info
  131.     if lAborted
  132.         ? "*Process aborted*"
  133.     endif
  134.     ? padl(ltrim(str(len(aFiles))), 10) + " file(s) " + padl(ltrim(str(nTotSize)),8) + " bytes used"
  135. return
  136.  
  137.  
  138. // This is not exactly a great error handler but it does the job
  139. procedure doBreak(e)
  140.     break e
  141. return
  142.