home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / FINDF.ZIP / FINDF.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1988-10-16  |  6.0 KB  |  259 lines

  1. {****************************************************************************
  2.  
  3.                                     FINDF
  4.  
  5.                            written by: Richard Simon
  6.  
  7.   SYSTEM: IBM PC/XT and AT compatibles running MS-DOS 3.xx
  8. LANGUAGE: Turbo PASCAL 5.0
  9.  
  10. LAST MODIFIED: October 15, 1988
  11.  
  12. COPYRIGHT
  13.  
  14.                   (C)copyright 1988 by Simon Say's Database Inc.
  15.                                all rights reserved
  16.  
  17. FINDF may not be sold in part or whole, it is strictly for example.  FINDF.EXE
  18. and the source code be be given away as long as it is not sold.
  19.  
  20. DESCRIPTION
  21.  
  22. FINDF is used to find ALL the files on the disk that match the search
  23. specification given to it from the command line and then report the findings
  24. back to the user.  FINDF also has the option of prompting the user to erase
  25. the files it finds, making it very useful for deleting all the files with
  26. the same name on your hard disk regardless of the subdirectory it is in.
  27.  
  28. *****************************************************************************}
  29.  
  30. {$R-,S+,I+,F-,O-,A+,V-,B+,D-,E-,L-}
  31.  
  32. PROGRAM Findf;
  33.  
  34. USES Crt,
  35.      Dos;
  36.  
  37. TYPE DirPtr = ^DirRec;
  38.      DirRec = record
  39.                 Name : pointer;
  40.                 Next : DirPtr;
  41.               end;
  42.  
  43. VAR Tree      : DirPtr;
  44.     CurBranch : DirPtr;
  45.     Delete    : boolean;
  46.     BeginHeap : pointer;
  47.  
  48.  
  49. PROCEDURE Introduction;
  50.  
  51. begin
  52.   writeln;
  53.   writeln('FINDF  (C)copyright 1988, Simon Say''s "Database" Inc.');
  54.   writeln;
  55. end;
  56.  
  57.  
  58. PROCEDURE Usage;
  59.  
  60. begin
  61.   writeln('USAGE: FINDF <search spec> </e>');
  62.   writeln;
  63.   writeln('   /e - User Prompted ERASE of files');
  64.   writeln;
  65.   Halt;
  66. end;
  67.  
  68.  
  69. PROCEDURE Abort;
  70.  
  71. begin
  72.   writeln;
  73.   writeln('Program Aborted by user...');
  74.   writeln;
  75.   Release(BeginHeap);
  76.   Halt;
  77. end;
  78.  
  79.  
  80. PROCEDURE AddToTree(Name : string);
  81.  
  82.   { Add each new Directory Name to the Tree }
  83.  
  84. var NewBranch : DirPtr;
  85.  
  86. begin
  87.   if Tree = nil then Mark(BeginHeap);
  88.  
  89.   new(NewBranch);
  90.   NewBranch^.Next := nil;
  91.  
  92.   GetMem(NewBranch^.Name,length(Name)+1);
  93.   Move(Name,NewBranch^.Name^,length(Name)+1);
  94.  
  95.   if Tree = nil then
  96.     begin
  97.       CurBranch := NewBranch;
  98.       Tree := NewBranch;
  99.     end
  100.   else
  101.     begin
  102.       CurBranch^.Next := NewBranch;
  103.       CurBranch := NewBranch;
  104.     end;
  105. end;
  106.  
  107.  
  108. PROCEDURE SearchTree(FileSpec : string);
  109.  
  110.   { SearchTree looks through ALL the directories found using the procedure
  111.     DIRECTORIES for the filespec given by the user and then reports the
  112.     findings back to the user.  If the Erase option was defined then the
  113.     user is also prompted for an OK to delete each file found. }
  114.  
  115. var DirName : string;
  116.     DirInfo : SearchRec;
  117.     Len     : byte;
  118.     WPtr    : DirPtr;
  119.     AnyFound: boolean;
  120.     Lines   : byte;
  121.  
  122.  
  123.     procedure More;
  124.     var Scan : char;
  125.     begin
  126.       writeln;
  127.       write('Press any key to continue..');
  128.       scan := ReadKey;
  129.  
  130.       if (scan = ^[) and (NOT Keypressed) then Abort;
  131.  
  132.       Lines := 0;
  133.       clrscr;
  134.       writeln('Files on Directory .. ',DirName,':'); inc(Lines);
  135.     end;
  136.  
  137.     procedure PromptDelete;
  138.     var Ans     : char;
  139.         DelFile : file;
  140.     begin
  141.       write('':13-length(DirInfo.Name),'Are you sure you want to erase (y/n)? ');
  142.       repeat
  143.         Ans := ReadKey;
  144.         if (Ans = ^[) and (NOT KeyPressed) then Abort;
  145.       until upcase(Ans) in['Y','N'];
  146.       if upcase(Ans) = 'Y' then
  147.       begin
  148.         assign(DelFile,DirName+'\'+DirInfo.Name);
  149.         {$I-} erase(DelFile); {$I+}
  150.         if IOresult <> 0 then
  151.           writeln('  Could NOT Erased!')
  152.         else writeln('   Erased');
  153.       end
  154.       else writeln('  NOT Erased!');
  155.     end;
  156.  
  157. begin
  158.   WPtr  := Tree;
  159.   Lines := 0;
  160.  
  161.   while WPtr <> nil do
  162.   begin
  163.     Move(WPtr^.Name^,Len,1);
  164.     Move(WPtr^.Name^,DirName,Len+1);
  165.  
  166.     AnyFound := FALSE;
  167.     chdir(DirName);
  168.  
  169.     findfirst(FileSpec,ARCHIVE,DirInfo);
  170.     while DosError = 0 do
  171.     begin
  172.       if Lines in[21,22] then More;
  173.       if NOT AnyFound then
  174.       begin
  175.         writeln('Files on Directory .. ',DirName,':'); inc(Lines);
  176.         AnyFound := TRUE;
  177.       end;
  178.       if Delete then
  179.         begin
  180.           write('    ',DirInfo.Name);
  181.           PromptDelete;
  182.         end
  183.       else
  184.         writeln('    ',DirInfo.Name);
  185.       inc(Lines);
  186.       findnext(DirInfo);
  187.     end;
  188.  
  189.     WPtr := WPtr^.Next;
  190.   end;
  191. end;
  192.  
  193.  
  194. PROCEDURE Directories(BeginPath : string);
  195.  
  196.    { Directoies is a recursive procedure that searches for ALL the directory
  197.      names on the current disk and adds them to a Tree in Memory for later
  198.      reference }
  199.  
  200. var DirInfo   : SearchRec;
  201.  
  202. begin
  203.   chdir(BeginPath);
  204.  
  205.   if BeginPath = '\' then BeginPath := '';
  206.  
  207.   findfirst('*.',DIRECTORY,DirInfo);
  208.   while DosError = 0 do
  209.   begin
  210.     if (DirInfo.Attr and Directory <> 0) and
  211.        (DirInfo.Name <> '.') and (DirInfo.Name <> '..') then
  212.     begin
  213.       AddToTree(BeginPath+'\'+DirInfo.Name);
  214.       Directories(BeginPath+'\'+DirInfo.Name);
  215.     end;
  216.     findnext(DirInfo);
  217.   end;
  218. end;
  219.  
  220.  
  221. PROCEDURE SearchDirectories;
  222.  
  223. var DefDir : string;
  224.     Options: string[2];
  225.  
  226. begin
  227.   if ParamCount < 1 then Usage;  { if no search paramater then show usage }
  228.  
  229.   Tree := nil;                   { Initialize tree }
  230.  
  231.   if ParamCount = 2 then         { Check for erase option }
  232.   begin
  233.     Options := ParamStr(2);
  234.     case Upcase(Options[2]) of
  235.       'E' : Delete := TRUE;
  236.     end;
  237.   end;
  238.  
  239.   GetDir(0,DefDir);              { Get Current working Directory }
  240.  
  241.   AddToTree('\');                { Add Root Directory to Tree }
  242.   Directories('\');              { Traverse Directory tree starting at Root }
  243.  
  244.   SearchTree(ParamStr(1));       { Search the Directory tree for ParamStr(1) }
  245.  
  246.   chdir(DefDir);                 { Change directory back to work directory }
  247.  
  248.   Release(BeginHeap);            { Release all memory allocated by FINDF }
  249. end;
  250.  
  251.  
  252. BEGIN
  253.  
  254.   CheckBreak := FALSE;     { Turn Ctrl-Break off }
  255.   Delete := FALSE;
  256.   Introduction;            { Show Logo }
  257.   SearchDirectories;
  258.  
  259. END.