Just a sample of the Echomail archive
Cooperative anarchy at its finest, still active today. Darkrealms is the Zone 1 Hub.
|    PASCAL_LESSONS    |    Pascal Programming Lessons    |    361 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 202 of 361    |
|    mark lewis to Robert Wolfe    |
|    Directory Listing    |
|    27 Nov 14 17:46:27    |
   
   On Thu, 27 Nov 2014, Robert Wolfe wrote to All:   
      
    RW> I am trying to get a listing of files in the currently logged   
    RW> directory and output those to a text file.   
      
   ok, that's simple enough...   
      
    RW> However, I cannot seem to figure out how to do this with the    
    RW> FindFirst() and FindNext() functions in FreePascal. The example I    
    RW> tried to base my own code on is as follows:   
      
   [chomp]   
      
    RW> Unfortunately, I was not able to get my example (which follows) to   
    RW> work:    
      
    RW> Procedure GenDirFile ;   
    RW> Var SR: TSearchRec ;   
    RW> DirList: TStrings;   
    RW> Path: String;   
    RW> DirFile: Text;   
    RW> Begin ;   
    RW> Assign(DirFile, 'directory.lst') ;   
    RW> Rewrite(DirFile) ;   
    RW> DirList := TStringsList.Create;   
    RW> if FindFirst('*.zip', faArchive, SR) = 0 then   
    RW> begin   
    RW> repeat   
    RW> WriteLn(DirFile, SR.Name);   
    RW> until FindNext(SR) <> 0;   
    RW> FindClose(SR);   
    RW> end;   
    RW> Close(DirFile);   
    RW> End ;   
      
    RW> Is there something I am doing wrong here?   
      
   ok, there's several things going on here... i won't step through all of them   
   but since you are attempting to use tstrings, let's go a little further and   
   use a tstringlist instead... mainly because tstrings is designed to be   
   inherited by other objects whch perform specialized tasks... tstringlist is   
   one such object...   
      
   the second thing is that you're not doing anything with the DirList that   
   you've created... you are, however, attempting to write the filenames directly   
   to your output file... that's ok but since we're using the tstringlist, all   
   that stuff is done for you...   
      
   there's a few other things but for now, let's look at this...   
      
   1. create our tstringlist object   
   2. fill our object with the files returned by findfirst/findnext   
   3. when all files are found, write our stringlist to a file   
      
   nice and easy... especially since the stringlist object already does a huge   
   amount of heavy lifting for us ;)   
      
   so here's the full code (!) of a working program... this was done on a vista   
   machine using FPC's trunk code (FPC 2.7.1) from their repository... i also   
   tested it on OS/2 eCS with FPC 2.6.4 and on a Linux with FPC 2.6.5 (the fixes   
   branch of FPC 2.6)...   
      
   ===== snip =====   
   program DirectoryList;   
      
   // we need the following unit libraries   
   uses Sysutils, Classes;   
   // Sysutils for findfirst and friends   
   // Classes for TStringList and friends   
      
   Procedure GenDirFile;   
    Var SR: TSearchRec;   
    DirList: TStringList;   
    Begin;   
    // create the class to hold the returned filenames   
    DirList := TStringList.Create;   
    // let's sort the list   
    DirList.Sorted := True;   
    if FindFirst('*.*', faArchive, SR) = 0 then   
    begin   
    repeat   
    // add the returned filename to the list   
    DirList.Add(SR.Name);   
    until FindNext(SR) <> 0;   
    // all done, close the find handle   
    FindClose(SR);   
    end;   
    // now let's write our list of filenames to a text file.   
    DirList.SaveToFile('directory.lst');   
    End ;   
      
   begin   
    GenDirFile;   
   end.   
   ===== snip =====   
      
    RW> Any help anyone can give would be greatly appreciated.   
      
   how's that? ;)   
      
      
   (!) please try to reduce the problem to the minimal code needed for a working   
   program that shows the failure or problem. partial code is ok in some cases   
   but that'll come later ;)   
      
   )\/(ark   
      
   If you think it's expensive to hire a professional to do the job, wait until   
   you hire an amateur.   
      
   --- FMail/Win32 1.60   
    * Origin: (1:3634/12.71)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca