Sign in to follow this  
Followers 0
GodlessSinner

How to delete last "x" bytes in file?

10 posts in this topic

I have a file (3.43GB) and need to remove the last 5 bytes, without copying first (3.43GB - 5bytes). How?

thanks.


_____________________________________________________________________________

Share this post


Link to post
Share on other sites



This is all I can think of. I haven't tested it, just wrote it here. Hope it works for you.

$file = FileOpen("filename",18)
$data = FileRead($file)
$bytes = StringSplit($data,"")
For $i = $bytes[0] - 6 To $bytes[0] - 1
$bytes[$i] = ""
Next
$new_data = _ArrayToString($bytes,"",1)
FileWrite($new_data)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

(Deleting my prev post)

Oh now I get what he means..

@dbz..

so u r assuming its a textfile? 3.43 gb large? kind of unlikely don't you think?

Edited by Manjish

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

Try

#include <WinAPI.au3>

$sFile = @ScriptDir & "\largefile"
$iTrim = 5    ; bytes to remove from end
$hFile = _WinAPI_CreateFile($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, FileGetSize($sFile) - $iTrim)
ConsoleWrite("Start " & $sFile & " " & FileGetSize($sFile) & " bytes" & @LF)
_WinAPI_SetEndOfFile($hFile)
_WinAPI_CloseHandle($hFile)
ConsoleWrite("Trimmed " & $sFile & " " & FileGetSize($sFile) & " bytes" & @LF)

Share this post


Link to post
Share on other sites

To @Manjish and @dbzfanatic, - Neither that, nor another:) I haven't enough free space to copy first 3.43Gb-5Bytes, and I haven't enough RAM to do that so, like you say..


_____________________________________________________________________________

Share this post


Link to post
Share on other sites

picaxe's version should work. It just opens the file,not copying it.

- Sould, but:

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe.....

Start E\Fedora-11-i386-DVD.iso 0 bytes

and then MsgBox(0, "Error", "_WinApi_CloseHandle: Incorrect descriptor.")


_____________________________________________________________________________

Share this post


Link to post
Share on other sites

- Sould, but:

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe.....

Start E\Fedora-11-i386-DVD.iso 0 bytes

and then MsgBox(0, "Error", "_WinApi_CloseHandle: Incorrect descriptor.")

Hi,

Start E\Fedora-11-i386-DVD.iso

Have a look at file name. There is a : missing

;-))

Stefan

Share this post


Link to post
Share on other sites

This is better and should work for files > 2^32 bytes

#include <WinAPI.au3>

$sFile = @ScriptDir & "\zzz.avi"
$iTrim = 5    ; bytes to remove from end
$hFile = _WinAPI_CreateFile($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, - $iTrim, 2)
ConsoleWrite("Start " & $sFile & " " & FileGetSize($sFile) & " bytes" & @LF)
_WinAPI_SetEndOfFile($hFile)
_WinAPI_CloseHandle($hFile)
ConsoleWrite("Trimmed " & $sFile & " " & FileGetSize($sFile) & " bytes" & @LF)

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