Sign in to follow this  
Followers 0
Terenz

Add-Delete byte prefix of file

3 posts in this topic

Hi,

I need to add-delete a string to a file without altering the structure of the file itselft. I'll explain with some images. This is the partial structure of a file:

2upr7vc.png

The expected result i want to have is:

hx8z8m.png

Add a string and not replace the exist. With this script:

$hFile = FileOpen(@ScriptDir & "\MyFile", 1)
If $hFile = -1 Then Exit
FileSetPos($hFile, 0, 0)
FileWrite($hFile, "ABC")
FileClose($hFile)

The result is this:

v3h9gk.png

Has you can see it replace the first three bytes and not add at the beginning of the file. So the first question is:

How to add byte to the beginning of the file? Obviously without load ALL the file in the RAM because it will crash with error allocating memory, i have already try it my PC isn't high end.

The second related question:

Ok, assuming i have add three new bytes to the beginning. How to remove it? Thanks for the help, i need this.


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites



My understanding is you cannot, at least no API exists for that, and you would need direct access to the file on disk, from which would arise, many more issues.

Read and re-write the file chunk by chunk, is the sensible way.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Step 1: Open all-new binary file for writing.

Step 2: Write your bytes to that.

Step 3: Open the file you want to alter for reading.

Step 4: Iterate over the bytes in the read file, write them to the write file.

Step 5: Close both file handles.

Step 6: Delete file you want to alter.

Step 7: Rename (and move if in different directory) your new file to the name (and path) of the old.

 

EDIT: More detailed version of JohnOne's answer. :)

Edited by mrider
1 person likes this

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0