* * *

Author Topic: Overwrite curent line  (Read 214 times)

samoraj

  • New member
  • *
  • Posts: 7
Overwrite curent line
« on: July 16, 2017, 07:51:35 pm »
Code: Pascal  [Select]
  1. procedure TPrint.Timer1Timer(Sender: TObject);
  2.  
  3.     var
  4.     searchResult : TSearchRec;
  5.     myfile, myfile1: text;
  6.     found : boolean;
  7.     sl : TStringList;
  8.     s: string;
  9.     i:integer;
  10.     count:integer;
  11.     File1, File2: TextFile;
  12.   begin
  13.     SetCurrentDir('C:\Users\Public\print\check');
  14.     if findfirst('*', faAnyFile, searchResult) = 0 then
  15.     begin
  16.       repeat
  17.         statictext4.Caption:=searchResult.Name;
  18.       until FindNext(searchResult) <> 0;
  19.     end;
  20.     if (searchResult.Name<> '.') and (searchResult.Name<> '..') then
  21.     begin
  22.        if  FileExists('C:\Users\Public\print\checkall\all.csv') then
  23.           begin
  24.            count := 0;
  25.            found := false;
  26.            sl := TStringList.Create;
  27.            filename1 := StaticText4.Caption;
  28.            filename2 := copy(filename1,0,(length(filename1)-4));
  29.           try
  30.              sl.LoadFromFile('C:\Users\Public\print\checkall\all.csv');
  31.                for line in sl do
  32.                if Pos(filename2, line)<>0 then
  33.                  begin
  34.  // hear i want to Overwrite curent line where "Pos(filename2, line)<>0" and Overwrite next five lines,
  35.  // with information from another textfile with 6 lines.
  36.                   system.Assign(myfile1, filename1);
  37.                    Reset(myfile1);
  38.                    system.Close(myfile1);
  39.                    Erase (myfile1); //  delete file
  40.                         break;
  41.                  end;
  42.                    finally
  43.                      sl.Free;
  44.  
  45.              end;
  46.            end;
  47.           end;
  48.     begin
  49.     SetCurrentDir('C:\Users\Public\print\check');
  50.     if findfirst('*', faAnyFile, searchResult) = 0 then
  51.     begin
  52.       repeat
  53.         statictext4.Caption:=searchResult.Name;
  54.       until FindNext(searchResult) <> 0;
  55.     end;
  56.     if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin
  57.  
  58.       //SERIAL MATHERBOARD
  59.      filename := StaticText4.Caption;
  60.      StaticText5.Caption:=filename;
  61.      if  FileExists(statictext4.Caption) then
  62.          begin
  63.  
  64.   assignfile(File1,'C:\Users\Public\print\check\'+statictext4.Caption);
  65.   assignfile(File2,'C:\Users\Public\print\checkall\all.csv');
  66.   reset(File1); //open for read;
  67.   append(File2); //asumes File2 can be written to, puts filepointer at end of the file
  68.   while not Eof(File1) do
  69.   begin
  70.     readln(File1,S); //read a line frome file1
  71.     writeln(File2,S); //write the line to file2
  72.   end;
  73.   CloseFile(File1);
  74.   CloseFile(File2);
  75.      Erase (File1); //  delete file
  76.       AssignFile(F, 'C:\Users\Public\print\checkall\all.csv');
  77.        Append(f);
  78.     CloseFile(F);
  79.      end;
  80.      end;
  81.  end;
  82. end;
  83.  

Hollo, i want to overwrite current line and next 5 lines on the text file with information from another text file if second text file name existing in first text file, an there where exist it's my current line. It's possible ?

taazz

  • Hero Member
  • *****
  • Posts: 3954
Re: Overwrite curent line
« Reply #1 on: July 16, 2017, 07:59:57 pm »
Only if the new lines have the same size as the old lines (overall).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

samoraj

  • New member
  • *
  • Posts: 7
Re: Overwrite curent line
« Reply #2 on: July 16, 2017, 08:34:10 pm »
the line can be 1 simbol , can be 100 simbol's

taazz

  • Hero Member
  • *****
  • Posts: 3954
Re: Overwrite curent line
« Reply #3 on: July 16, 2017, 08:56:28 pm »
you already have the first file in memory.
Code: Pascal  [Select]
  1. procedure TPrint.Timer1Timer(Sender: TObject);
  2.  
  3.     var
  4.     searchResult : TSearchRec;
  5.     myfile, myfile1: text;
  6.     found : boolean;
  7.     sl : TStringList;
  8.     s: string;
  9.     i:integer;
  10.     count:integer;
  11.     File1, File2: TextFile;
  12.     vCntr : integer;//nothing new just a counter.
  13.   begin
  14.     SetCurrentDir('C:\Users\Public\print\check');
  15.     if findfirst('*', faAnyFile, searchResult) = 0 then
  16.     begin
  17.       repeat
  18.         statictext4.Caption:=searchResult.Name;
  19.       until FindNext(searchResult) <> 0;
  20.     end;
  21.     if (searchResult.Name<> '.') and (searchResult.Name<> '..') then
  22.     begin
  23.        if  FileExists('C:\Users\Public\print\checkall\all.csv') then
  24.           begin
  25.            count := 0;
  26.            found := false;
  27.            sl := TStringList.Create;
  28.            filename1 := StaticText4.Caption;
  29.            filename2 := copy(filename1,0,(length(filename1)-4));
  30.           try
  31.              sl.LoadFromFile('C:\Users\Public\print\checkall\all.csv');
  32.                for vCntr := 0 to sl.count -1 do begin //use a for loop instead.
  33.                  if Pos(filename2, sl[Vcntr])<>0 then begin
  34.                    //read the first line from the other file in the line variable
  35.                    sl[Vcntr] := line; //replace the existing line with the new
  36.                    //read the second line from the other file in the line variable
  37.                    sl[vCntr+1] := line; //replace the second line with the new
  38.                    //read the third line from the other file in the line variable
  39.                    sl[vCntr+2] := line; //replace the third line with the new
  40.                    //read the fourth line from the other file in the line variable
  41.                    sl[vCntr+3] := line; //replace the fourth line with the new
  42.                    //read the fifthline from the other file in the line variable
  43.                    sl[vCntr+4] := line; //replace the fifth line with the new
  44.                    break; //exit the for loop;
  45.                  end;
  46.                end;
  47.                  sl.savetofile(StaticText4.Caption);//replace the file data with the new data.
  48.                    finally
  49.                      sl.Free;
  50.               end;
  51.            end;
  52.           end;
  53.     begin
  54.     SetCurrentDir('C:\Users\Public\print\check');
  55.     if findfirst('*', faAnyFile, searchResult) = 0 then
  56.     begin
  57.       repeat
  58.         statictext4.Caption:=searchResult.Name;
  59.       until FindNext(searchResult) <> 0;
  60.     end;
  61.     if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin
  62.  
  63.       //SERIAL MATHERBOARD
  64.      filename := StaticText4.Caption;
  65.      StaticText5.Caption:=filename;
  66.      if  FileExists(statictext4.Caption) then
  67.          begin
  68.  
  69.   assignfile(File1,'C:\Users\Public\print\check\'+statictext4.Caption);
  70.   assignfile(File2,'C:\Users\Public\print\checkall\all.csv');
  71.   reset(File1); //open for read;
  72.   append(File2); //asumes File2 can be written to, puts filepointer at end of the file
  73.   while not Eof(File1) do
  74.   begin
  75.     readln(File1,S); //read a line frome file1
  76.     writeln(File2,S); //write the line to file2
  77.   end;
  78.   CloseFile(File1);
  79.   CloseFile(File2);
  80.      Erase (File1); //  delete file
  81.       AssignFile(F, 'C:\Users\Public\print\checkall\all.csv');
  82.        Append(f);
  83.     CloseFile(F);
  84.      end;
  85.      end;
  86.  end;
  87. end;
  88.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus