Hi,
I´m don´t know if this is a simple or complicated question.
I build this script, copy pasting from posts of this forum.
I´m trying to replace a string in a binary file. The string I want to replace is "SMT_MAIN.DRW". The script replaces the string well.
If I replace the string for one with the same number of characters, everything works great.
But if the replacement string is longer or shorter, binary file becomes invalid.
Can this be easily solved ?
I attached the binary file toosmt_main.7z
#include <String.au3>
$sInFile = @ScriptDir & "\SMT_MAIN.drw" ; Original in file
$Orig = "SMT_MAIN.PRT"
$sFind = _StringToHex($Orig)
$Insert = "SMT_MAI4.PRT"
$sReplace = _StringToHex($Insert)
$sOutFile = @ScriptDir & "\SMT_MAIN1.DRW" ; New edited out file
_BinaryReplace($sInFile, $sFind, $sReplace, $sOutFile)
Func _BinaryReplace($sInFile, $sFind, $sReplace, $sOutFile)
Local $FO, $FR
$FO = FileOpen($sInFile, 16) ; Open the file in binary read mode
$FR = FileRead($FO) ; Read the opened file into a variable
FileClose($FO) ; Close the opened file now we've read it
$FR = StringReplace($FR, $sFind, $sReplace, 1); find and replace the hex string
$FO = FileOpen($sOutFile, 18) ; Open a new empty file in binary write mode
FileWrite($FO, $FR) ; Write the data to the new opened file
FileClose($FO) ; Close the newly created file.
EndFunc