home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Snippets / ScanDirectory / ScanDirectory.p < prev   
Encoding:
Text File  |  1994-05-06  |  1.9 KB  |  60 lines  |  [TEXT/R*ch]

  1. {------------------------------------- Scan Directory -------------------------------------}
  2. {This unit contains a single procedure ScanDir.  Passing this a FSSpec will search all the files and}
  3. {folders in that directory.  It calls its the searching procedure ScanSubDir recursively forr each}
  4. {sub directory in the initial directory until it has searched all files and folders.  Add your own}
  5. {code at the point below where it finds individual files.  Like most things this code was made}
  6. {possible by learning from others, I just put it all together in one unit for my own purposes and}
  7. {thought others might take advantage of it.}
  8. {}
  9. {Chris Owen}
  10. {owen-christopher@yale.edu}
  11. {-----------------------------------------------------------------------------------------}
  12. unit ScanDir;
  13.  
  14. interface
  15.  
  16.     procedure ScanDir (firstDir: FSSpec);
  17.  
  18. implementation
  19.  
  20.     procedure ScanDir (firstDir: FSSpec);
  21.         var
  22.             name: Str255;
  23.             theCPRec: CInfoPBRec;
  24.             theErr: OSErr;
  25.  
  26.         procedure ScanSubDir (theDir: longint);
  27.             var
  28.                 index: integer;
  29.  
  30.         begin
  31.             index := 1;
  32.             repeat
  33.                 name := '';
  34.                 theCPRec.ioFDirIndex := index;
  35.                 theCPRec.ioDrDirID := theDir; {have to reset the dirnum each time through}
  36.                 theErr := PBGetCatInfo(@theCPRec, FALSE);
  37.  
  38.                 if theErr = noErr then
  39.                     if BitTst(@theCPRec.ioFlAttrib, 3) then
  40.                         begin {If bit 4 is set then it is a folder}
  41.                             ScanSubDir(theCPRec.ioDrDirID); {call recursively to index next folder}
  42.                             theErr := 0;  {eventually this returns an error so reset it}
  43.                         end
  44.                     else
  45.                         begin {else it is a file}
  46.                 {Do whatever you do when you find a file}
  47.                         end; {else}
  48.                 index := index + 1;
  49.             until (theErr <> noErr);
  50.         end;
  51.  
  52.     begin
  53.         with theCPRec do {set up the CPRec with the vRefNum and place to store file name}
  54.             begin
  55.                 ioNamePtr := @name;
  56.                 ioVRefNum := firstDir.vRefNum;
  57.             end; {with}
  58.         ScanSubDir(firstDir.parID); {search first directory, call recursively to do rest}
  59.     end;
  60. end.