Ultratuga Posted September 25, 2014 Share Posted September 25, 2014 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 Link to comment Share on other sites More sharing options...
JohnOne Posted September 26, 2014 Share Posted September 26, 2014 I don't know much about this, but if the bytes are more or less, then you;re overwriting or removing existing bytes. Cannot be good. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Ultratuga Posted September 26, 2014 Author Share Posted September 26, 2014 Maybe you´re right, and this can´t be done. I was thinking in trying to split the file in three parts, the string, the code before and the code after. Then create a new file with code before string &replacement String &code after string, but I doubt this will work... Link to comment Share on other sites More sharing options...
Gianni Posted September 26, 2014 Share Posted September 26, 2014 Maybe you´re right, and this can´t be done. I was thinking in trying to split the file in three parts, the string, the code before and the code after. Then create a new file with code before string &replacement String &code after string, but I doubt this will work... there is no need to do that, the file is already shrinked or expanded according to the size of the replaced string (there isn't overlay of bytes), the problem that arise when the replaced string has different length maybe is due to the fact that previous data, no longer has the same previous offset location within the file, but it's at a shifted location, and this perhaps will render some pointers within the file to point to an invalidated location. Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
junkew Posted September 27, 2014 Share Posted September 27, 2014 If i look at the file its always on this line ending with a continuation character in the binary and only seems to occur 1 time in the file #- CMNM 00csmt_main.drw \ So I would suggest to padd to equal length replacement string by adding spaces FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Gianni Posted September 27, 2014 Share Posted September 27, 2014 If i look at the file its always on this line ending with a continuation character in the binary and only seems to occur 1 time in the file #- CMNM 00csmt_main.drw \ So I would suggest to padd to equal length replacement string by adding spaces Good idea if replacing string is shorter. Problem remains if the replacing string is longer than the replaced. Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
junkew Posted September 27, 2014 Share Posted September 27, 2014 Spaces enough after the string to replace just padd your searchstr with spaces FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now