Recent

Author Topic: Overwrite curent line  (Read 5782 times)

samoraj

  • New Member
  • *
  • Posts: 22
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: 5368
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: 22
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: 5368
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

samoraj

  • New Member
  • *
  • Posts: 22
Re: Overwrite curent line
« Reply #4 on: July 22, 2017, 02:43:58 am »
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('C:\Users\Public\print\checkall\all.csv');//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.  
I cange line 47. with sl.savetofile('C:\Users\Public\print\checkall\all.csv');
When i create file filename2 second time the program not replace lines, delete old lines and in new lines recording. How i fix this ?
http://alfa.kachi-snimka.info/images-2013/opt1500684110g.jpg
http://alfa.kachi-snimka.info/images-2013/arx1500684157i.jpg
i want to replace existing lines.
« Last Edit: July 22, 2017, 02:48:21 am by samoraj »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Overwrite curent line
« Reply #5 on: July 22, 2017, 03:09:56 am »
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('C:\Users\Public\print\checkall\all.csv');//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.  
I cange line 47. with sl.savetofile('C:\Users\Public\print\checkall\all.csv');
When i create file filename2 second time the program not replace lines, delete old lines and in new lines recording. How i fix this ?
http://alfa.kachi-snimka.info/images-2013/opt1500684110g.jpg
http://alfa.kachi-snimka.info/images-2013/arx1500684157i.jpg
i want to replace existing lines.
Code: Pascal  [Select][+][-]
  1.   begin
  2.     SetCurrentDir('C:\Users\Public\print\check');
  3.     if findfirst('*', faAnyFile, searchResult) = 0 then begin
  4.       repeat
  5.         statictext4.Caption:=searchResult.Name;
  6.       until FindNext(searchResult) <> 0;
  7.     end;
  8.  
  9.     if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin
  10.       //SERIAL MATHERBOARD
  11.       filename := StaticText4.Caption;
  12.       StaticText5.Caption:=filename;
  13.       if  FileExists(statictext4.Caption) then begin
  14.         assignfile(File1,'C:\Users\Public\print\check\'+statictext4.Caption);
  15.         assignfile(File2,'C:\Users\Public\print\checkall\all.csv');
  16.         reset(File1); //open for read;
  17.         append(File2); //asumes File2 can be written to, puts filepointer at end of the file
  18.         while not Eof(File1) do begin
  19.           readln(File1,S); //read a line frome file1
  20.           writeln(File2,S); //write the line to file2
  21.         end;
  22.         CloseFile(File1);
  23.         CloseFile(File2);
  24.         Erase (File1); //  delete file
  25.         AssignFile(F, 'C:\Users\Public\print\checkall\all.csv');
  26.         Append(f);
  27.         CloseFile(F);
  28.       end;
  29.     end;
  30.   end;
  31.  
why are you keep on processing the same files below? try deleting the above code.
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: 22
Re: Overwrite curent line
« Reply #6 on: July 22, 2017, 03:48:32 am »
Because filename1 and first line in filename1 its Identical . If my file name is test.txt , my first line in file test.txt is 'test' and i want to checking if file name exist in
'C:\Users\Public\print\checkall\all.csv' then to replace a old lines with begin line file name with text information on filename.

samoraj

  • New Member
  • *
  • Posts: 22
Re: Overwrite curent line
« Reply #7 on: July 22, 2017, 04:02:45 am »
I cheching configuration on laptops and everyone laptop saving his configuration on one file in network (buffer directory 'C:\Users\Public\print\check') with name matherboard name. My program geting this file and saving in another file 'C:\Users\Public\print\checkall\all.csv' . If buffer file name exist in 'C:\Users\Public\print\checkall\all.csv' like text, i want to replace information in curent line on 'C:\Users\Public\print\checkall\all.csv' with a new information from (buffer directory 'C:\Users\Public\print\check') , because the laptop configuration may be a diferent (ram, hdd, odd).

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Overwrite curent line
« Reply #8 on: July 22, 2017, 09:49:12 am »
I cheching configuration on laptops and everyone laptop saving his configuration on one file in network (buffer directory 'C:\Users\Public\print\check') with name matherboard name. My program geting this file and saving in another file 'C:\Users\Public\print\checkall\all.csv' . If buffer file name exist in 'C:\Users\Public\print\checkall\all.csv' like text, i want to replace information in curent line on 'C:\Users\Public\print\checkall\all.csv' with a new information from (buffer directory 'C:\Users\Public\print\check') , because the laptop configuration may be a diferent (ram, hdd, odd).
In your code, only one random file is processed (the last one in the enumeration). But you need everything. And, although this is not said, I suppose that if there is no source information, then it needs to be added. Then better something like this:
Code: Pascal  [Select][+][-]
  1. procedure AddOrReplaceInList(InList: TStringList; const ContainsPart: string;
  2.   NewLines: TStringList);
  3. var
  4.   IndexOfStart: Integer;
  5.   i: Integer;
  6. begin
  7.   IndexOfStart := -1;
  8.   for i := InList.Count - 1 downto 0 do
  9.     if Pos(ContainsPart, InList[i]) > 0 then
  10.     begin
  11.       IndexOfStart := i;
  12.       InList.Delete(i);
  13.     end;
  14.   if IndexOfStart < 0 then
  15.     IndexOfStart := InList.Count;
  16.   for i := NewLines.Count - 1 downto 0 do
  17.     InList.Insert(IndexOfStart, NewLines[i]);
  18. end;
  19.  
  20. procedure AppendNewInfo(const FromDir: string; const InFile: string);
  21. var
  22.   NewFiles: TStringList;
  23.   AllData: TStringList;
  24.   AppenedLines: TStringList;
  25.   FullFileName, NameOnly: string;
  26. begin
  27.   NewFiles := FileUtil.FindAllFiles(FromDir, '', False, 0);
  28.   try
  29.     if NewFiles.Count = 0 then
  30.       Exit;
  31.     AllData := TStringList.Create;
  32.     try
  33.       AllData.LoadFromFile(InFile);
  34.       AppenedLines := TStringList.Create;
  35.       try
  36.         for FullFileName in NewFiles do
  37.         begin
  38.           AppenedLines.LoadFromFile(FullFileName);
  39.           NameOnly := ExtractFileName(FullFileName);
  40.           AddOrReplaceInList(AllData, NameOnly, AppenedLines);
  41.         end;
  42.       finally
  43.         AppenedLines.Free;
  44.       end;
  45.       AllData.SaveToFile(InFile);
  46.     finally
  47.       AllData.Free;
  48.     end;
  49.     for FullFileName in NewFiles do
  50.       DeleteFile(FullFileName);
  51.   finally
  52.     NewFiles.Free;
  53.   end;
  54. end;
  55. //...
  56. AppendNewInfo('C:\Users\Public\print\check', 'C:\Users\Public\print\checkall\all.csv');

samoraj

  • New Member
  • *
  • Posts: 22
Re: Overwrite curent line
« Reply #9 on: July 22, 2017, 07:48:44 pm »
After change configuration on laptop and create file with the same name , because the matherboard on the laptop is the same , only configuration is diferent , i need to replace all line with old configuration. But your script save the new configuration on next free line.
http://alfa.kachi-snimka.info/images-2013/klz1500744264w.jpg //file with first gonfiguration
http://alfa.kachi-snimka.info/images-2013/bwy1500744325f.jpg //file with second configuration
http://alfa.kachi-snimka.info/images-2013/viu1500744351h.jpg //file where i save information from all files

After checking configuration on two laptops i have http://alfa.kachi-snimka.info/images-2013/viu1500745258j.jpg
When i change configuration on the one of the laptops i havehttp://alfa.kachi-snimka.info/images-2013/opt1500745013t.jpg
but i want http://alfa.kachi-snimka.info/images-2013/bfi1500745547o.jpg
Old configuration to be replaced from new configuration.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Overwrite curent line
« Reply #10 on: July 22, 2017, 10:10:03 pm »
After change configuration on laptop and create file with the same name , because the matherboard on the laptop is the same , only configuration is diferent , i need to replace all line with old configuration. But your script save the new configuration on next free line.
http://alfa.kachi-snimka.info/images-2013/klz1500744264w.jpg //file with first gonfiguration
http://alfa.kachi-snimka.info/images-2013/bwy1500744325f.jpg //file with second configuration
http://alfa.kachi-snimka.info/images-2013/viu1500744351h.jpg //file where i save information from all files

After checking configuration on two laptops i have http://alfa.kachi-snimka.info/images-2013/viu1500745258j.jpg
When i change configuration on the one of the laptops i havehttp://alfa.kachi-snimka.info/images-2013/opt1500745013t.jpg
but i want http://alfa.kachi-snimka.info/images-2013/bfi1500745547o.jpg
Old configuration to be replaced from new configuration.
Why not delete the old all.csv and recreate it?
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: 22
Re: Overwrite curent line
« Reply #11 on: July 23, 2017, 12:46:19 am »
Because i need to delete old information only for curent laptop. I don't andarstent many and dis is my logic . And if deleti old file possible to lose other information. i don't no...

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Overwrite curent line
« Reply #12 on: July 23, 2017, 01:21:05 am »
@samoraj:
Is the output format you use for the combined .csv file mandatory ?

I ask, because the gathered combined information is a) not stored in .csv format b) difficult to search/replace.

If you really want it the way you say you want then i am able to come up with a solution, but it is far from ideal because there are many things that could go wrong.

fwiw: The code you showed so far makes no sense to me what-so-ever (but that could be just me not understanding what it is you try to do).

samoraj

  • New Member
  • *
  • Posts: 22
Re: Overwrite curent line
« Reply #13 on: July 23, 2017, 01:56:04 am »
No problem to be txt

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Overwrite curent line
« Reply #14 on: July 23, 2017, 03:46:01 am »
Sorry for the delay (i had to do something else first).

This is my take at things:
Code: Pascal  [Select][+][-]
  1. procedure Update;
  2. const
  3.   {$IFDEF MOLLYS_COMPUTER}
  4.   BasePath = 'H:\Lazarus-forums\37580\';
  5.   {$ELSE}
  6.   BasePath = 'C:\Users\';
  7.   {$ENDIF}
  8.   GatherFile = BasePath + 'Public\print\checkall\all.csv';
  9. var
  10.   // Directory where Motherboard information is stored
  11.   MoBoDir       : String = BasePath + 'Public\print\check';
  12.   MoBoFiles     : TStringList;  // List containing found Motherboard Filenames
  13.   MoBoFileIndex : integer;      // Index to iterate MoBoFiles list
  14.  
  15.   MoBoContents  : TStringList;  // List with contents of a Motherboard file
  16.   FieldIndex    : integer;      // index to iterate MoBoContents list
  17.   MoBoIdentifier: string;       // Motherboard identifier
  18.   MoBoValues    : String;       // Motherboard additional values
  19.  
  20.   GatherContents: TStringList;  // List that contaisn combined contents of Motherboards
  21. begin
  22.   MoBoFiles := TStringList.Create;
  23.  
  24.   // Find all Motherboard filenames
  25.   FindAllFiles(MoBoFiles, MoBoDir, '*', false);
  26.  
  27.   // If there are any motherboard files then we need to continue
  28.   if (MoBoFiles.Count > 0) then
  29.   begin
  30.     GatherContents := TStringList.Create;
  31.     GatherContents.NameValueSeparator := #9;  // Separated by tab
  32.  
  33.     // Load pre-existing combined file in case it exists
  34.     if FileExists(GatherFile)
  35.       then GatherContents.LoadFromFile(GatherFile);
  36.  
  37.     // process each MotherBoard file
  38.     for MoBoFileIndex := 0 to Pred(MoBoFiles.Count) do
  39.     begin
  40.       // Load the contents of the Motherboard file
  41.       MoBoContents := TStringList.Create;
  42.       MoBoContents.LoadFromFile(MoBoFiles[MoBoFileIndex]);
  43.  
  44.       //
  45.       // process contents of Motherboard file
  46.       //
  47.       MoBoValues := '';
  48.       // Process each and every line in a motherboard file
  49.       for FieldIndex := 0 to Pred(MoBoContents.Count) do
  50.       begin
  51.         // The first line is always the motherboad identifier
  52.         if FieldIndex = 0 then MoBoIdentifier := MoBoContents[FieldIndex]
  53.         else
  54.         // the following lines contain additional motherboard information
  55.         // copy them one-by-one into string MoBoVlaues, separated by a tab
  56.         // character
  57.         begin
  58.           MoBoValues := MoBoValues + MoBoContents[FieldIndex];
  59.           if FieldIndex < Pred(MoBoContents.Count)
  60.           then MoBoValues := MoBoValues + #9;
  61.         end;
  62.       end;
  63.  
  64.       // Add/Replace motherboard information in the gathered combined list
  65.       GatherContents.Values[MoBoIdentifier] := MoBoValues;
  66.  
  67.       // Free the Motherboard contents list from memory
  68.       MoBoContents.Free;
  69.     end;
  70.  
  71.     // Save the updated gathered information back to file
  72.     GatherContents.SaveToFile(GatherFile);
  73.    
  74.     // Free the list with gathered informationt
  75.     GatherContents.Free;
  76.   end;
  77.  
  78.   // Free the list with motherboard filenames
  79.   MoBoFiles.Free;
  80. end;
  81.  

 

TinyPortal © 2005-2018