Sign in to follow this  
Followers 0
AzKay

Removing characters from the end of a file?

9 posts in this topic

Ive wanted to make a function to add/remove 0x0 from files, until the size is X.

Ive got;

$File = ""
$Bytes = ""

Do
    $File = FileOpen($File, 1+16)
    FileWrite($File, 0)
    FileClose($File)
Until FileGetSize($File) == $Bytes

But, this only makes the file bigger.

I want to do the reverse, remove the last characters until its X size.


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites



$FilePAth = ""
$Bytes = ""
$File = FileOpen($FilePAth, 1+16)
$File2 = FileOPen($FilePAth&".tmp", 1+16)
FileWrite($File2,FileRead($File,$bytes)) ; Reads Given amount of bytes and writes them to an new File
FileClose($file)
FileClose($file2)
FileDelete($FilePATH)
FileMove($FilePATH&".tmp",$FILEPATH)


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Thanks :3


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites

Know of a more memory efficient way?

I just tried it on an 800mb file, and itll just hang.

Also, using making the computer go really slow.

Requiring me to restart, because nor taskmanager, or process explorer, would kill it.


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites

Hmm, I ended up using some VB6 code.

If anyones interested, I could rewrite it in autoit.


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites

Know of a more memory efficient way?

Bunch of DllCalls to WinAPI functions: CreateFile+SetFilePointer+SetEndOfFile+CloseHandle

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Maybe something like this:

Func _FileSplit($sFilename, $iOutfileBytes)
    Local $hInfile, $iInfileBytes, $sFile, $i=0
    $iInfileBytes = FileGetSize($sFilename)

    If $iInfileBytes > $iOutfileBytes Then
        $hInfile = FileOpen($sFilename, 16)
        $sFile = FileRead($hInfile, $iOutfileBytes)
        While @error <> -1
            $i += 1
            FileWrite(StringFormat($sFilename & ".%04d",$i), $sFile)
            $sFile = FileRead($hInfile, $iOutfileBytes)
        WEnd
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Bunch of DllCalls to WinAPI functions: CreateFile+SetFilePointer+SetEndOfFile+CloseHandle

Yeah, Thats the VB6 code I used.

Not quite working out in autoit though, It keeps giving me "unknown revision", Or something along those lines.


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites

K, Scrapped half the DLLCalls, Founds them in the _WinAPI_ functions.

Used those instead, Finnished product:

#Include <WinAPI.au3>

SetFileSize("test.txt", 6)

Func SetFileSize($FileName, $newSize)
    $fileHandle = _WinAPI_CreateFile($FileName, 2)
    DllCall("kernel32.dll", "long", "SetFilePointer", "long", $fileHandle, "long", $newSize, "long", 0, "long", 0)
    DllCall("kernel32.dll", "long", "SetEndOfFile", "long", $fileHandle)
    _WinAPI_CloseHandle($fileHandle)
EndFunc

:3


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

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