home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / ins_msb / 9203 / treewalk.bas < prev    next >
BASIC Source File  |  1992-02-06  |  5KB  |  125 lines

  1. ' TreeWalk.Bas - Support module for the TreeWalk routines.
  2.  
  3. CONST FALSE = 0, TRUE = NOT FALSE
  4.  
  5. ' $INCLUDE: 'TREEWALK.BI'
  6. ' $INCLUDE: 'DIRFUN.BI'
  7. ' $INCLUDE: 'QUALNAME.BI'
  8. ' $INCLUDE: 'PARSPATH.BI'
  9. ' $INCLUDE: 'CURDIR.BI'
  10. ' $INCLUDE: 'CHDRIVE.BI'
  11.  
  12. ' The ProcessDirectoryEntry routine declared below is contained in 
  13. ' the main module of the program using the TreeWalk support module.
  14. ' Each time you write a program to use TreeWalk, you'll need to write
  15. ' your own version of ProcessDirectoryEntry.
  16. DECLARE SUB ProcessDirectoryEntry (PathSpec$, EntrySpec$, _
  17.             DirEnt AS DirectoryRecord, Level%)
  18.  
  19.  
  20. SUB TreeWalk (Spec$, ActionKey%)
  21. ' TreeWalk - Iterate all the files in the tree
  22. '            starting with the path Spec$
  23.  
  24. WhereIWasWhenIStarted$ = CurDir$    ' Save current directory
  25.  
  26. ' Qualify and parse the user's input specification
  27.  
  28. ParsePathname Spec$, JustDrive$, JustPath$, JustSpec$  ' parse input spec
  29. ChDrive JustDrive$                    ' Change to specified drive
  30. WhereDriveWasWhenItStarted$ = CurDir$ ' save cwd on specified drive
  31. CHDIR JustPath$                       ' enter the starting directory
  32.  
  33. ' Since we pass the "\" separator into the real tree walking code,
  34. ' we need to make sure we're not starting with the root directory.
  35. ' If we are, the path spec already ends with a "\".
  36. FirstSep$ = ""
  37. IF RIGHT$(JustPath$, 1) <> "\" THEN FirstSep$ = "\"
  38.  
  39. ' Begin the treewalk.
  40. TreeWalkHelper JustPath$ + FirstSep$, JustSpec$, ActionKey%, 1
  41.  
  42. CHDIR WhereDriveWasWhenItStarted$     ' reset cwd on specified drive
  43.  
  44. ' Parse the original working directory into drive and path,
  45. ' and return there.
  46. ParsePathname WhereIWasWhenIStarted$, JustDrive$, JustPath$, JustSpec$
  47. ChDrive JustDrive$: CHDIR WhereIWasWhenIStarted$
  48. ' End of TreeWalk
  49. END SUB
  50.  
  51. SUB TreeWalkHelper (PathSpec$, EntrySpec$, ActionKey%, Level%)
  52. ' TreeWalkHelper - Subprogram to perform the essential details
  53. '                  of a recursive directory treewalk.
  54.  
  55. DIM twDTA AS DataTransferArea
  56. DIM DirEnt AS DirectoryRecord
  57. DIM Stat AS INTEGER
  58.  
  59. ' Set up the data transfer area for this level of TreeWalkHelper
  60. SetDTA twDTA
  61.  
  62. ' Find the first entry in the current directory that matches
  63. ' the input specification. You can modify fAttr% below to filter
  64. ' out entries you don't want to process, (such as hidden & system files),
  65. ' Attribute value     Meaning
  66. '         &H00         Normal file
  67. '         &H01         Read-only
  68. '         &H02         Hidden
  69. '         &H04         System
  70. '         &H08         Volume label
  71. '         &H10         Directory/subdirectory
  72. '         &H20         Archived
  73. fAttr% = 2 OR 4 OR &H10
  74.  
  75. Stat = FindFirst(fAttr%, "*.*", DirEnt, twDTA)
  76. DO WHILE Stat = 0
  77.   ' Extract the filename from the directory entry record.
  78.   EntryName$ = _
  79.        MID$(DirEnt.FileName, 1, INSTR(DirEnt.FileName, CHR$(0)) - 1)
  80.   ' If we're doing a top-down traversal, then we'll process the
  81.   ' directory entry name here and now. Ideally, you'd use the
  82.   ' top-down traversal to print all the names of files, count the
  83.   ' number of files, accumulate a total size in bytes, etc.
  84.   IF (ActionKey% AND TopDown) THEN
  85.     ' However, if the directory entry is the one for either
  86.     ' either the current or parent directories, ignore it. If
  87.     ' you wre creating a TREE-oriented version of the DIR command,
  88.     ' then you might NOT want to ignore these two special entries.
  89.     IF EntryName$ <> "." AND EntryName$ <> ".." THEN
  90.       ' Process a directory entry here. Or, rather, invoke
  91.       ' the subprogram ProcessDirectoryEntry, which resides
  92.       ' in your program's main source module.      
  93.       ProcessDirectoryEntry PathSpec$, EntrySpec$, DirEnt, Level%
  94.     END IF
  95.   END IF
  96.   ' If this directory entry is a subdirectory instead of a file,
  97.   ' then it's time to recurse to the next level of the tree.
  98.   IF DirEnt.FileAttb AND 16 THEN
  99.     ' However, we'll get stuck in an infinite loop if we try
  100.     ' to enter the current directory or the parent directory this way.
  101.     IF EntryName$ <> "." AND EntryName$ <> ".." THEN
  102.       CHDIR EntryName$    ' Enter the subdirectory
  103.       TreeWalkHelper PathSpec$ + EntryName$ + "\", _
  104.                      EntrySpec$, ActionKey%, Level% + 1
  105.       CHDIR ".."          ' Exit from the subdirectory
  106.     END IF
  107.   END IF
  108.   ' When we recursed to the next level, it's activation changed the
  109.   ' data transfer area to a new one for it's level. Now we have to
  110.   ' change the DTA back to our own.
  111.   SetDTA twDTA
  112.   ' If we're performing a bottoms-up traversal, then we process this
  113.   ' entry after having "gone into it" (if it was a subdirectory).
  114.   IF (ActionKey% AND BottomsUp) THEN
  115.     IF EntryName$ <> "." AND EntryName$ <> ".." THEN
  116.       ProcessDirectoryEntry PathSpec$, EntrySpec$, DirEnt, Level%
  117.     END IF
  118.   END IF
  119.   ' Lastly, we need to see if there's another entry in this directory
  120.   ' that requires processing.
  121.   Stat = FindNext(DirEnt, twDTA)
  122. LOOP
  123. END SUB
  124.  
  125.