home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ss.zip / ss.pas < prev   
Pascal/Delphi Source File  |  1996-11-05  |  5KB  |  123 lines

  1. Program SS;
  2. uses dos,crt;
  3.  
  4. var x,y,z: integer;
  5.    uprcase, replace, chng, verbose : boolean;
  6.    ttt, yyy, filespec, searchstring, replacestring, curntline, cul: string; /*ttt and yyy is holdstrings */
  7.    curntfile, otf: text;
  8.   DirInfo: TSearchRec;
  9. /*========-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  10.  
  11. Procedure ErrExit;   { Clean exit procedure }
  12.  Begin
  13.    TextBackground(2);
  14.    Writeln('SuperSearch v1.0.os2 - Text Search & Replace inside files by E.S. Dunckley 1994/9   ');
  15.    TextBackground(0);
  16.    Writeln('SS.EXE filespec searchstring replacesting [/c]                                  ');
  17.    Writeln;
  18.    Writeln('  filespec       -  A valid filespec eg. *.ini or w*.bat');
  19.    Writeln('  searchstring   -  Any textstring NOT containing spaces');
  20.    Writeln('  replacestring  -  Any textstring NOT containting spaces');
  21.    Writeln('  /c             -  Do a case sensitive search (and replace)');
  22.    Writeln('  /v             -  Run in Verbose mode');
  23.    Writeln;
  24.    Writeln('eg. SS *.ini C: D:            {Replaces all C:''s to D:''s in *.ini}');
  25.    Writeln('    SS A*.txt mystake mistake {Replaces a typo in all docs starting with A. }');
  26.    Halt(1);
  27.  End;
  28. /*========-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  29. Begin
  30.   uprcase:=False;
  31.   verbose:=false;
  32.   replace:=true;
  33.  
  34.   x:=paramcount;
  35.   if (x<2) then begin                        {Too little parameters to make sense }
  36.     Writeln('Error. Needs at least 2 parameters to process');
  37.     ErrExit;
  38.     End;
  39.   filespec:=paramstr(1);
  40.   searchstring:=paramstr(2);
  41.   replacestring:=paramstr(3);
  42.   if ((paramstr(4)='/v') or (replacestring='/v') or (paramstr(5)='/v')) then verbose:=true;
  43.   if ((paramstr(4)='/V') or (replacestring='/V') or (paramstr(5)='/V')) then verbose:=true;
  44.   if not ((replacestring='/C') or (paramstr(4)='/C') or (paramstr(5)='/C')
  45.          or (replacestring='c') or (paramstr(5)='c') or (paramstr(4)='c')) then begin
  46.      for z:=1 to length(searchstring) do searchstring[z]:=upcase(searchstring[z]);
  47.      if verbose then writeln('Running in UPPERCASE mode');
  48.      uprcase:=True;
  49.   end;
  50.   WriteLn('SearchString: ',searchstring);
  51.   if (replacestring='/c') or (replacestring='/C') or (replacestring='') then begin
  52.      replacestring:='';
  53.      if verbose then writeln('Running without replacing files - no replace string given');
  54.      replace:=false;
  55.   end;
  56.   if (uprcase and replace) then for z:=1 to length(replacestring) do replacestring[z]:=upcase(replacestring[z]);
  57.   if replace then writeln('Replace String: ',replacestring);
  58.  
  59.  
  60.   if (filespec='/?') or (filespec='/h') or (filespec='/H') or (filespec='-?') then begin
  61.      Writeln('SS.EXE - Search (and replace) ascii strings in files');
  62.      ErrExit;
  63.   End;
  64.  
  65. /*=Mainloop=======-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  66.  
  67.   FindFirst(filespec, Anyfile, DirInfo);
  68.   while DosError = 0 do
  69.   begin
  70.     Assign(curntfile,DirInfo.Name);
  71.     if replace then Assign(Otf,'$$SS.tmp');
  72.     if replace then Rewrite(otf);
  73.     Reset(curntfile);
  74.     if verbose then writeln('Opening file');
  75.     Chng:=false;
  76.     While not EOF(curntfile) do begin
  77.      Readln(curntfile,curntline);
  78.      cul:=curntline;
  79.      if uprcase then for z:=1 to length(cul) do cul[z]:=upcase(cul[z]);
  80.      if (pos(searchstring,cul)>0) then writeln(dirinfo.name,': ',curntline);
  81.      while (pos(searchstring,cul)>0) and (replace) do begin
  82.       Chng:=true;
  83.       y:=pos(searchstring,cul);
  84.       if replace then begin
  85.         ttt:=concat(copy(curntline,1,y-1));
  86. /*        writeln(ttt); */
  87.         yyy:=copy(curntline,y+length(searchstring),length(curntline)-y-length(searchstring)+1);
  88. /*        writeln(yyy); */
  89.       end;
  90.       if replace then curntline:=concat(ttt,replacestring,yyy);
  91. /* curntline:=concat(copy(curntline,1,y-1),replacestring,copy(curntline,y+length(searchstring),length(curntline)-y-length(searchstring)+1)); */
  92.       if replace then writeln(curntline);
  93.       cul:=curntline;
  94.       if uprcase then for z:=1 to length(cul) do cul[z]:=upcase(cul[z]);
  95.      end;
  96.      if replace then writeln(otf,curntline);
  97.     end;
  98.     close(curntfile);
  99.     if verbose then writeln('Closing file');
  100.     if replace then begin
  101.        close(otf);
  102.        if chng then begin
  103.           if verbose then writeln('File needs updating, rewriting file');
  104.           Assign(curntfile,dirinfo.name);
  105.           rewrite(curntfile);
  106.           Assign(otf,'$$SS.tmp');
  107.           reset(otf);
  108.           while not eof(otf) do begin
  109.             readln(otf,curntline);
  110.             writeln(curntfile,curntline);
  111.           end;
  112.           close(otf);
  113.           close(curntfile);
  114.           chng:=false;
  115.        end;
  116.     end;
  117.  
  118.     FindNext(DirInfo); /* Locate the next file to update */
  119.   end;
  120. end.
  121.  
  122.  
  123.