home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18606 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  3.7 KB

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!ucbvax!ucdavis!burrito.engr.ucdavis.edu!cklarson
  2. From: cklarson@burrito.engr.ucdavis.edu (Christopher Klaus Larson)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Access directory contents
  5. Message-ID: <19385@ucdavis.ucdavis.edu>
  6. Date: 18 Nov 92 21:02:45 GMT
  7. References: <1992Nov16.131617.2096@ncsu.edu>
  8. Sender: usenet@ucdavis.ucdavis.edu
  9. Distribution: comp.sys.mac.programmer
  10. Organization: College of Engineering - University of California - Davis
  11. Lines: 105
  12.  
  13. In article <1992Nov16.131617.2096@ncsu.edu> cpastore@unity.ncsu.edu (Chris Pastore) writes:
  14. >
  15. >Is there some easy way to get the listing of file names once a
  16. >directory is identified?
  17. >
  18.  
  19. Call PBGetCatInfo on the directory: once with ioFDirIndex negative, to
  20. obtain the number of files in the directory; then call it again, once for
  21. each value of ioFDirIndex between 1 and the number of files in the
  22. directory, inclusive. (I know this explination is not very clear, 
  23. basically you are performing an indexed search of the directory.)
  24.  
  25. Source code for this method follows.
  26.  
  27. This can also be accomplished with PBCatSearch, although for using
  28. PBGetCatInfo is easier for this task (IMHO of course :-)) and probably
  29. faster in this case, since we don't have to search the entire volume.
  30.  
  31. Both PBGetCatInfo and PBCatSearch are explained in IM:Files.
  32.  
  33. --Chris
  34. cklarson@engr.ucdavis.edu
  35.  
  36. Source (THINK C) follows, as always, comments, bug fixes, etc. welcome
  37. -------------
  38.  
  39. /*******************************************************
  40.  
  41.    GetAllFiles -- Given a volume reference # (volume)
  42.                   and a directory ID (directory)
  43.                   this builds a FSSpec for each file
  44.                   (NOT Directories) within the
  45.                   given directory. NOTE: it does
  46.                   nothing with the FSSpec -- that is
  47.                   for you to add.
  48.  
  49. *******************************************************/
  50. void GetAllFiles (short volume, long directory)
  51. {
  52.     CInfoPBRec        pb;
  53.     short            num_files,index;
  54.     Boolean            isDir,ignored;
  55.     FSSpec            current_file;
  56.     Str255            directory_name;
  57.     
  58.     /* Call once to determine # of files. Note that the name
  59.        of the directory is returned in directory_name. The
  60.        variable only exists to provide space for the returned
  61.        value -- we don't need the name for anything. */
  62.  
  63.     pb.dirInfo.ioCompletion = 0;
  64.     pb.dirInfo.ioDrDirID = directory;
  65.     pb.dirInfo.ioVRefNum = volume;
  66.     pb.dirInfo.ioNamePtr = directory_name;
  67.     pb.dirInfo.ioFDirIndex = -1;
  68.     
  69.     if (PBGetCatInfo (&pb,false))
  70.         return; /* return on error */
  71.     
  72.     /* pb.dirInfo.ioDrNmFls now contains the number of files in our
  73.        directory */
  74.  
  75.     num_files = pb.dirInfo.ioDrNmFls;
  76.  
  77.     /* Now get info for each file in the directory, one at a time */
  78.  
  79.     for (index = 1;index <= num_files; index++)
  80.         {
  81.  
  82.         /* The volume reference # and directory could be changed
  83.            by the call to ResolveAliasFile() later in the loop,
  84.            so they must be reset each time through to be correct. */
  85.  
  86.                current_file.parID = directory;
  87.             current_file.vRefNum = volume;
  88.  
  89.         /* Set up Paramblock for indexed call...*/
  90.  
  91.         pb.hFileInfo.ioCompletion = 0;
  92.         pb.hFileInfo.ioNamePtr = current_file.name;
  93.         pb.hFileInfo.ioVRefNum = volume;
  94.         pb.hFileInfo.ioFDirIndex = index;
  95.         pb.hFileInfo.ioDirID = directory;
  96.         
  97.         /* Make the call and return on an error. */
  98.  
  99.         if (PBGetCatInfo (&pb,false))
  100.             break;
  101.         
  102.         /* If the file is an alias, this will resolve it,
  103.            otherwise it will have no effect. */
  104.  
  105.         ResolveAliasFile (¤t_file,true,&isDir,&ignored);
  106.  
  107.         /* If it's a directory, ignore it */
  108.         if (!isDir)
  109.             {
  110.             
  111.             /* At this point, current_file contains information
  112.                about one of the files in our directory. Do
  113.                whatever is necessary to the file here. */
  114.  
  115.             }
  116.         }
  117. }
  118.