Jump to content

robertjohns

Members
  • Content Count

    74
  • Joined

  • Last visited

Everything posted by robertjohns

  1. robertjohns

    Find, Replace and Save

    How can I find all instances of a string in a file , replace all instance of the string and save the copy of the file in Delphi
  2. robertjohns

    Find, Replace and Save

    Thanks, any plan to provide any working example with multiple search and replacement with faster method
  3. robertjohns

    Find, Replace and Save

    Already provided working procedure by @PeterBelow , it works like charm as per my expectations only the problem is it gives error if file size is more than 2GB . Out of memory while expanding memory stream procedure ReplaceBytesInFile(aStream: TMemoryStream; const aSearchBytes, aReplaceBytes: TBytes); var LNumBytes: Integer; LPos, LEnd: PByte; begin LNumBytes := Length(aSearchBytes); Assert(LNumBytes = Length(aReplaceBytes), 'Arrays have to be of the same length!'); if LNumBytes = 0 then Exit; LPos := aStream.Memory; LEnd := LPos; Inc(LEnd, aStream.Size - LNumBytes ); while LPos < LEnd do begin if LPos^ = aSearchBytes[0] then begin if CompareMem(LPos, @aSearchBytes[0], LNumBytes) then begin CopyMemory(LPos, @aReplaceBytes[0], LNumBytes); // or // Move( aReplaceBytes[0], LPos^, LNumBytes ); Inc(LPos, LNumBytes); end else Inc(LPos); end else Inc(LPos); end; {while} end; type TDataRec = record SearchFor, ReplaceBy: TBytes end; const Data: array [0..1] of TDataRec = ( ( SearchFor: [$74, $00, $6f, $00, $7, $00, $73]; ReplaceBy: [$68, $00, $6f, $00, $7, $00, $73]), ( SearchFor: [$76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]; ReplaceBy: [$00, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]) // add more data as needed, adjust array upper bound accordingly ); procedure TForm2.Test; var I: Integer; theFile: string; LBuffer: TMemoryStream; begin theFile := 'pathname here'; LBuffer := TMemoryStream.Create(); try LBuffer.LoadFromFile(theFile); for I := Low(Data) to High(Data) do ReplaceBytesInFile(LBuffer, Data[I].SearchFor, Data[I].ReplaceBy); LBuffer.SaveToFile(theFile); finally LBuffer.Free; end; end;
  4. robertjohns

    Find, Replace and Save

    I am still waiting for your help to solve the big file issue please
  5. robertjohns

    Find, Replace and Save

    No not directly replacement of the original file but to create the copy of it.
  6. robertjohns

    Find, Replace and Save

    I am really making you panic even I am trying on Intel(R) Core(TM) i7-7700HQ 16GB RAM and 2TB of SSD and using Original Windows 10 64bit . I am really surprised if it works on more 5GB of File why on my PC gives Out of memory while expanding memory stream. on 2.5GB of file. Is there any way to make Change of TMemoryStream to TFileStream in your procedure ? because this procedure is working as my expections only the problem is bigger file size
  7. robertjohns

    Find, Replace and Save

    The Procedure works well on 2GB file but If the File size is above 2GB it gives error, Out of memory while expanding memory stream. I am using MSWindows 10 64bit and Delphi 10.2
  8. robertjohns

    Find, Replace and Save

    If aStream is TFileStream then how to use LPos := aStream.Memory;
  9. robertjohns

    Find, Replace and Save

    It is 32 bit Platform and the file size is 3GB
  10. robertjohns

    Find, Replace and Save

    Thanks a lot for your help but I get error Out of memory while expanding memory stream.
  11. robertjohns

    Find, Replace and Save

    Waiting for @PeterBelow response, he definitely may be having any easy logic like his previous function.
  12. robertjohns

    Find, Replace and Save

    I have a binary file n which I need to make changes.
  13. robertjohns

    Find, Replace and Save

    Thanks .. No Actually I am confused with var theFile: string; SearchFor, ReplaceWith: TBytes; begin SearchFor := [$76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]; ReplaceWith := [$00, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $5F]; ReplaceBytesInFile(theFile, SearchFor, ReplaceWith); $76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00 $5F, $00, $74, $00, $7A, $00, $5F, $00, $00 $77, $00, $68, $00, $79, $00, $70, $00, $5F, $00, $00 $44, $10, $6D, $00, $6F, $00, $64, $00, $65, $00, $6D, $00, $5F, $00, $00 $69, $10, $00, $00, $6C, $00, $75, $00, $65, $00, $74, $00, $6F, $00, $6F, $00, $74, $00, $68, $00, $5F, $00, $00 $6F, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $73, $00, $65, $00, $63, $00, $61, $00, $70, $00, $70, $00, $5F, $00, $00 $65, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $5F, $00, $00 $00, $10, $61, $00, $00, $00, $6C, $00, $5F, $00, $00 $34, $10, $64, $00, $73, $00, $70, $00, $5F, $00, $00 $8C, $10, $6B, $00, $65, $00, $79, $00, $6D, $00, $61, $00, $73, $00, $74, $00, $65, $00, $72, $00, $5F, $00, $00 $3B, $00, $00, $00, $6F, $00, $6F, $00, $74, $00, $5F, $00, $00 How can declare other hex lines for variable SearchFor, ReplaceWith: TBytes; Thanks in advance
  14. robertjohns

    Find, Replace and Save

    Will ReplaceBytesInFile function work for multiple hex changes like If I try to replace all these hex lines by other hex $76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00 $5F, $00, $74, $00, $7A, $00, $5F, $00, $00 $77, $00, $68, $00, $79, $00, $70, $00, $5F, $00, $00 $44, $10, $6D, $00, $6F, $00, $64, $00, $65, $00, $6D, $00, $5F, $00, $00 $69, $10, $00, $00, $6C, $00, $75, $00, $65, $00, $74, $00, $6F, $00, $6F, $00, $74, $00, $68, $00, $5F, $00, $00 $6F, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $73, $00, $65, $00, $63, $00, $61, $00, $70, $00, $70, $00, $5F, $00, $00 $65, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $5F, $00, $00 $00, $10, $61, $00, $00, $00, $6C, $00, $5F, $00, $00 $34, $10, $64, $00, $73, $00, $70, $00, $5F, $00, $00 $8C, $10, $6B, $00, $65, $00, $79, $00, $6D, $00, $61, $00, $73, $00, $74, $00, $65, $00, $72, $00, $5F, $00, $00 $3B, $00, $00, $00, $6F, $00, $6F, $00, $74, $00, $5F, $00, $00
  15. robertjohns

    Find, Replace and Save

    Thanks the code works but I am sorry I did not explained my question properly . yes '.' represents 00 in hex viewer . HEX value for t.o.p.s is 74 00 6f 00 70 00 73 and HEX value for h.o.p.s is 68 00 6f 00 70 00 73 so I have to change hex value from 74 to 68 value should be changed by hex method not tops to hops Thanks in advance
  16. robertjohns

    Find, Replace and Save

    StringReplace method will not work for this .. because we need to loop and find the all similar occurrences of t.o.p.s in whole the files and all the found occurrences need to be replaced by t to h and then after change need to save it.
  17. robertjohns

    Find, Replace and Save

    Thanks , no it is not a text file it is binary file where I need to search all instances of t.o.p.s and need to replace only t as h in all the instances by inc method and save the file
  18. robertjohns

    Deleting string wich does include number

    I am using Delphi 10 Is there any way to delete a string which does not contain number with it like abc def23 fgr65 kbt idopt87 How can I delete the bold one because it does contain number with it Thanks in advance
  19. robertjohns

    Deleting string wich does include number

    Thanks PaPaNi Really appreciable solution and help my problem solved, thanks again
  20. robertjohns

    ListView Items additions

    I need to add Listview items from 2 Memo's , I am using Delphi 10.2 Tokyo Memo1 lines are as line1 line2 line3 line4 line5 Memo2 lines are as something1 something2 something3 something4 something5 Need to add these 2 Memo lines into Listview Listview Caption = Memo1 lines Listview SubItem = Memo2 lines Need help to achieve this Thanks in advance
  21. robertjohns

    ListView Items additions

    Thanks a lot problem solved
  22. robertjohns

    ListView Items additions

    Thanks again for your reply . loading from file it is easy but I need it from 2 memo lines and it seems harder to achieve it especially for me May some will come out with solution and help me here
  23. robertjohns

    ListView Items additions

    Thanks for reply SL.LoadFromFile('Logger11.csv'); but the I am seeking help about using Memo1 lines as Listview Caption and Memo2 lines as Listview subitem
  24. robertjohns

    need help to change the hex in binary file

    I am new to Delphi and I need help from experts to change the hex value to 00 00 00 for selected text shown in image https://i.ibb.co/9WwYc2Z/Screenshot-1.png I will be highly thankful for kind help
×