richietheprogrammer Posted May 18, 2011 Posted May 18, 2011 Hi everyone, I am trying to search for text in a bin file (hex), and I am trying to replace the text (all without actually opening the file). So I am guessing I have to fileopen it in hex. I also found filesetpos, which doesnt really help me because I am not sure if the offset is consistent. So i want to search, and replace in hex. Any help is much appreciated
hannes08 Posted May 18, 2011 Posted May 18, 2011 (edited) ;File test.au3 $fh = FileOpen("test.au3", 16) $SH = FileRead($fh) FileClose($fh) $SH = StringReplace($SH, "246668", "000000") $fh = FileOpen("test2.au3", 17) FileWrite($fh, $SH) FileClose($fh) This will replace "$fh" with "NUL NUL NUL". But you can't replace text in a file without opening it. Doesn't make sense to me... Edited May 18, 2011 by Hannes123 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 18, 2011 Author Posted May 18, 2011 On 5/18/2011 at 6:33 PM, 'Hannes123 said: ;File test.au3 $fh = FileOpen("test.au3", 16) $SH = FileRead($fh) FileClose($fh) $SH = StringReplace($SH, "246668", "000000") $fh = FileOpen("test2.au3", 17) FileWrite($fh, $SH) FileClose($fh) This will replace "$fh" with "NUL NUL NUL". But you can't replace text in a file without opening it. Doesn't make sense to me... Hmm thanks for this. What I meant by open it is I dont want to physically open it, and hit ctrl f..... What you provided looks like what I wanted. But, the file has "1212121212". I want to replace that by, lets say, 2323232323. As of now, nothing is changing when I run your script Any idea why? Thanks again
hannes08 Posted May 18, 2011 Posted May 18, 2011 (edited) On 5/18/2011 at 6:42 PM, 'richietheprogrammer said: Hmm thanks for this. What I meant by open it is I dont want to physically open it, and hit ctrl f..... What you provided looks like what I wanted. But, the file has "1212121212". I want to replace that by, lets say, 2323232323. As of now, nothing is changing when I run your script Any idea why? Thanks again Did you change the filenames to the correct files?You can print the contents of what you read if you add ConsoleWrite($SH & @CRLF) after line 3 and 4, so you'll see the difference immediately.With this you could also ensure that there is the "1212121212" in the original file. Edited May 18, 2011 by Hannes123 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 18, 2011 Author Posted May 18, 2011 On 5/18/2011 at 6:46 PM, 'Hannes123 said: Did you change the filenames to the correct files?You can print the contents of what you read if you add ConsoleWrite($SH & @CRLF) after line 3 and 4, so you'll see the difference immediately.With this you could also ensure that there is the "1212121212" in the original file.I see that it changed when I add ConsoleWrite($SH & @CRLF). But it is not saving. How can I save it as a different name (just prompt for file name).
hannes08 Posted May 18, 2011 Posted May 18, 2011 On 5/18/2011 at 6:51 PM, 'richietheprogrammer said: I see that it changed when I add ConsoleWrite($SH & @CRLF). But it is not saving. How can I save it as a different name (just prompt for file name).Take a look at the second FileOpen() you'll need to set the right filename there, too.You can prompt for the filename with InputBox for example or more comfortable: FileOpenDialog. Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 18, 2011 Author Posted May 18, 2011 On 5/18/2011 at 6:56 PM, 'Hannes123 said: Take a look at the second FileOpen() you'll need to set the right filename there, too.You can prompt for the filename with InputBox for example or more comfortable: FileOpenDialog.Yes i know this. I have created another file, and I am choosing it, but still it is not changing in there. Question: does the script look for the hex or dec in the file? Which means if I want to replace 1111, do I look for 1111 or 31 31 31 31 ?
hannes08 Posted May 18, 2011 Posted May 18, 2011 On 5/18/2011 at 6:58 PM, 'richietheprogrammer said: Yes i know this. I have created another file, and I am choosing it, but still it is not changing in there. Question: does the script look for the hex or dec in the file? Which means if I want to replace 1111, do I look for 1111 or 31 31 31 31 ?It just treats the bin data as a string. So look for 1111. In my example "246668" were the first three characters of the file (in this case "$fh"). Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 18, 2011 Author Posted May 18, 2011 On 5/18/2011 at 7:03 PM, 'Hannes123 said: It just treats the bin data as a string. So look for 1111. In my example "246668" were the first three characters of the file (in this case "$fh"). ok thanks. Here is my code: $fh = FileOpen("C:\Documents and Settings\user\Desktop\old", 16) $SH = FileRead($fh) FileClose($fh) $SH = StringReplace($SH, "36393639363936393639", "2222222222222222222") ConsoleWrite($SH & @CRLF) $fh = FileOpen("C:\Documents and Settings\user\Desktop\new", 17) FileWrite($fh, $SH) FileClose($fh) The console is showing the "22.." but the "new" file still has "3639.." ? Thanks again!
hannes08 Posted May 18, 2011 Posted May 18, 2011 $fh = FileOpen("test2.au3", 2) FileWrite($fh, "0x001122334455667788993639363936393639363999887766554433221100") FileClose($fh) $fh = FileOpen("test2.au3", 16) $SH = FileRead($fh) FileClose($fh) ConsoleWrite($SH & @CRLF) $SH = StringReplace($SH, "36393639363936393639", "22222222222222222222") ConsoleWrite($SH & @CRLF) $fh = FileOpen("test3.au3", 18) FileWrite($fh, $SH) FileClose($fh) Works perfectly for me. The only error I found was that your searchstring had 20 characters and your replacestring had 19 so it would write a text file. Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 18, 2011 Author Posted May 18, 2011 On 5/18/2011 at 7:06 PM, 'richietheprogrammer said: ok thanks. Here is my code: $fh = FileOpen("C:\Documents and Settings\user\Desktop\old", 16) $SH = FileRead($fh) FileClose($fh) $SH = StringReplace($SH, "36393639363936393639", "2222222222222222222") ConsoleWrite($SH & @CRLF) $fh = FileOpen("C:\Documents and Settings\user\Desktop\new", 17) FileWrite($fh, $SH) FileClose($fh) The console is showing the "22.." but the "new" file still has "3639.." ? Thanks again! i figured out what it is doing. It is appending it to the end of the file, but it is writing them in string format. I want it to look the same, so how can i write them back in hex?
hannes08 Posted May 18, 2011 Posted May 18, 2011 Saw my last post? 1. Use FileOpen("File", 18) 2. Make sure searchstring and replacestring have the same length. Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
richietheprogrammer Posted May 19, 2011 Author Posted May 19, 2011 On 5/18/2011 at 7:27 PM, 'Hannes123 said: Saw my last post?1. Use FileOpen("File", 18)2. Make sure searchstring and replacestring have the same length. Thanks a lot for your help
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