ArxBzFhblxphS Posted April 27, 2009 Share Posted April 27, 2009 (edited) Edited August 9, 2009 by Thanubis Link to comment Share on other sites More sharing options...
PsaltyDS Posted April 27, 2009 Share Posted April 27, 2009 I'm working on a file converter, and I'm now stuck on a couple of conversions.I need to add a 541 byte header to the beginning of a file without overwriting anything. For the reverse, I need to delete those 541 bytes. I'd like to do it without reading the old file and writing it into a new one. Is that possible either with this UDF or another that I don't know about yet?If the file is small enough to read entirely to memory, then you can modify it there and write back to the same file. That doesn't allow you to verify a successful write before wiping out the old file, though. So why would you want to do it that way? Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
PsaltyDS Posted April 27, 2009 Share Posted April 27, 2009 The maximum file size would be 512KB (524,288 Bytes). I'm just looking for whatever method is the fastest for my purposes. I wasn't actually planning on checking for a successful write because I'm not expecting write errors, but I suppose I should.I don't know if it makes a difference or not, but I'm planning to FileCopy the file that needs to be converted, and then make the edits to the copy. The original file will remain unchanged.1. Open the new file for binary write2. Write your binary prepending data3. Open the old file for binary read4. Write the old file data into the new file5. Close both files6. If the write was successful, delete the old file and rename the new to the old file name.If the file doesn't get changed by other processes, then you could rename and keep the old file. Then just swapping the file name back gets the original file restored without any read/write file ops. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Mobius Posted April 27, 2009 Share Posted April 27, 2009 (edited) This is my Fugly way of doing it.WARNING - Overwrites target file!!!!!So running this script on a file will add the header if not detected,Or will remove the header if detected.expandcollapse popup#REGION CMD IF $Cmdline[0] <> 0 AND FileExists($Cmdline[1]) THEN IF _DHEAD($Cmdline[1]) THEN ConsoleWrite("Operations for: "& @LF & $Cmdline[1] & @LF &"Complete! ;-)") ELSE ConsoleWrite(@SCRIPTNAME &" Failure :-( "& @LF) EXIT 1 ENDIF ELSE ConsoleWrite(@SCRIPTNAME &" <TargetFile>"& @LF) ENDIF EXIT #ENDREGION #REGION UDF FUNC _DHEAD($sFile) IF FileExists($sFile) THEN LOCAL CONST $vHeader = _ "242424240000000000000000000000000000000000"& _ "000000000000000000000000000000000000000000" LOCAL $iHlen = StringLen($vHeader) LOCAL $iSize = Int($iHlen / 2) LOCAL $hFile = FileOpen($sFile,16) IF $hFile = -1 THEN RETURN SetError(2,0,0) LOCAL $bFile = FileRead($hFile) IF @ERROR THEN RETURN SetError(3,0,0) FileClose($hFile) IF StringInStr(StringMid($bFile,3,$iHlen),$vHeader) THEN ConsoleWrite("Header found, Deleting"& @LF) $bFile = BinaryMid(Binary($bFile),$iSize + 1) ELSE ConsoleWrite("Header not found, Appending"& @LF) $bFile = "0x"& $vHeader & StringTrimLeft($bFile,2) ENDIF $hFile = FileOpen($sFile,18) IF $hFile = -1 THEN RETURN SetError(4,0,0) $bFile = FileWrite($sFile,Binary($bFile)) FileClose($hFile) IF $bFile = 0 THEN RETURN SetError(5,0,0) RETURN 1 ELSE RETURN SetError(1,0,0) ENDIF ENDFUNC #ENDREGIONDid not see your FileCopy statement.... but you get the idea! Vlad Edited April 27, 2009 by Mobius Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 28, 2009 Share Posted April 28, 2009 If you want to insert data at the beginning of the file, the only way is to completely rewrite it. File systems do not support prepending data. Just for the record. 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