you already have the first file in memory.I cange line 47. with sl.savetofile('C:\Users\Public\print\checkall\all.csv');
procedure TPrint.Timer1Timer(Sender: TObject); var searchResult : TSearchRec; myfile, myfile1: text; found : boolean; sl : TStringList; s: string; i:integer; count:integer; File1, File2: TextFile; vCntr : integer;//nothing new just a counter. begin SetCurrentDir('C:\Users\Public\print\check'); if findfirst('*', faAnyFile, searchResult) = 0 then begin repeat statictext4.Caption:=searchResult.Name; until FindNext(searchResult) <> 0; end; if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin if FileExists('C:\Users\Public\print\checkall\all.csv') then begin count := 0; found := false; sl := TStringList.Create; filename1 := StaticText4.Caption; filename2 := copy(filename1,0,(length(filename1)-4)); try sl.LoadFromFile('C:\Users\Public\print\checkall\all.csv'); for vCntr := 0 to sl.count -1 do begin //use a for loop instead. if Pos(filename2, sl[Vcntr])<>0 then begin //read the first line from the other file in the line variable sl[Vcntr] := line; //replace the existing line with the new //read the second line from the other file in the line variable sl[vCntr+1] := line; //replace the second line with the new //read the third line from the other file in the line variable sl[vCntr+2] := line; //replace the third line with the new //read the fourth line from the other file in the line variable sl[vCntr+3] := line; //replace the fourth line with the new //read the fifthline from the other file in the line variable sl[vCntr+4] := line; //replace the fifth line with the new break; //exit the for loop; end; end; sl.savetofile('C:\Users\Public\print\checkall\all.csv');//replace the file data with the new data. finally sl.Free; end; end; end; begin SetCurrentDir('C:\Users\Public\print\check'); if findfirst('*', faAnyFile, searchResult) = 0 then begin repeat statictext4.Caption:=searchResult.Name; until FindNext(searchResult) <> 0; end; if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin //SERIAL MATHERBOARD filename := StaticText4.Caption; StaticText5.Caption:=filename; if FileExists(statictext4.Caption) then begin assignfile(File1,'C:\Users\Public\print\check\'+statictext4.Caption); assignfile(File2,'C:\Users\Public\print\checkall\all.csv'); reset(File1); //open for read; append(File2); //asumes File2 can be written to, puts filepointer at end of the file while not Eof(File1) do begin readln(File1,S); //read a line frome file1 writeln(File2,S); //write the line to file2 end; CloseFile(File1); CloseFile(File2); Erase (File1); // delete file AssignFile(F, 'C:\Users\Public\print\checkall\all.csv'); Append(f); CloseFile(F); end; end; end; end;
you already have the first file in memory.I cange line 47. with sl.savetofile('C:\Users\Public\print\checkall\all.csv');
procedure TPrint.Timer1Timer(Sender: TObject); var searchResult : TSearchRec; myfile, myfile1: text; found : boolean; sl : TStringList; s: string; i:integer; count:integer; File1, File2: TextFile; vCntr : integer;//nothing new just a counter. begin SetCurrentDir('C:\Users\Public\print\check'); if findfirst('*', faAnyFile, searchResult) = 0 then begin repeat statictext4.Caption:=searchResult.Name; until FindNext(searchResult) <> 0; end; if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin if FileExists('C:\Users\Public\print\checkall\all.csv') then begin count := 0; found := false; sl := TStringList.Create; filename1 := StaticText4.Caption; filename2 := copy(filename1,0,(length(filename1)-4)); try sl.LoadFromFile('C:\Users\Public\print\checkall\all.csv'); for vCntr := 0 to sl.count -1 do begin //use a for loop instead. if Pos(filename2, sl[Vcntr])<>0 then begin //read the first line from the other file in the line variable sl[Vcntr] := line; //replace the existing line with the new //read the second line from the other file in the line variable sl[vCntr+1] := line; //replace the second line with the new //read the third line from the other file in the line variable sl[vCntr+2] := line; //replace the third line with the new //read the fourth line from the other file in the line variable sl[vCntr+3] := line; //replace the fourth line with the new //read the fifthline from the other file in the line variable sl[vCntr+4] := line; //replace the fifth line with the new break; //exit the for loop; end; end; sl.savetofile('C:\Users\Public\print\checkall\all.csv');//replace the file data with the new data. finally sl.Free; end; end; end; begin SetCurrentDir('C:\Users\Public\print\check'); if findfirst('*', faAnyFile, searchResult) = 0 then begin repeat statictext4.Caption:=searchResult.Name; until FindNext(searchResult) <> 0; end; if (searchResult.Name<> '.') and (searchResult.Name<> '..') then begin //SERIAL MATHERBOARD filename := StaticText4.Caption; StaticText5.Caption:=filename; if FileExists(statictext4.Caption) then begin assignfile(File1,'C:\Users\Public\print\check\'+statictext4.Caption); assignfile(File2,'C:\Users\Public\print\checkall\all.csv'); reset(File1); //open for read; append(File2); //asumes File2 can be written to, puts filepointer at end of the file while not Eof(File1) do begin readln(File1,S); //read a line frome file1 writeln(File2,S); //write the line to file2 end; CloseFile(File1); CloseFile(File2); Erase (File1); // delete file AssignFile(F, 'C:\Users\Public\print\checkall\all.csv'); Append(f); CloseFile(F); end; end; end; end;
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/opt1500684110g.jpg)
http://alfa.kachi-snimka.info/images-2013/arx1500684157i.jpg (http://alfa.kachi-snimka.info/images-2013/arx1500684157i.jpg)
i want to replace existing lines.
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:
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.Why not delete the old all.csv and recreate it?
http://alfa.kachi-snimka.info/images-2013/klz1500744264w.jpg (http://alfa.kachi-snimka.info/images-2013/klz1500744264w.jpg) //file with first gonfiguration
http://alfa.kachi-snimka.info/images-2013/bwy1500744325f.jpg (http://alfa.kachi-snimka.info/images-2013/bwy1500744325f.jpg) //file with second configuration
http://alfa.kachi-snimka.info/images-2013/viu1500744351h.jpg (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 (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 (http://alfa.kachi-snimka.info/images-2013/opt1500745013t.jpg)
but i want http://alfa.kachi-snimka.info/images-2013/bfi1500745547o.jpg (http://alfa.kachi-snimka.info/images-2013/bfi1500745547o.jpg)
Old configuration to be replaced from new configuration.