Jump to content

Script problem - remove header by 32 bytes and copy with different file extension


 Share

Recommended Posts

Hi guys!

I'm trying to figure out how to adapt this script to do the following:

1) Copy all .pvr files into the local folder to an /export folder

2) Remove 32 bytes in the header, and rename files into .bin

 

; this will read a file to memory, remove the first 256 bytes from it and save the result to a new file

$file_src= $sExtension = ".pvr" ; source file
$file_dest="/exported/*.bin" ; destination file (chunked)

$chunk_size=32 ; number of bytes to remove, thus the $start value for BinaryMid() (+1 to [b]start[/b], for instance, in byte no. 257)
$count=FileGetSize($file_src)-$chunk_size ; this will be the new filesize, thus the $count value for BinaryMid()

$handle_src=FileOpen($file_src,16) ; open in binary read mode
$handle_dest=FileOpen($file_dest,16+2) ; open new file in binary WRITE mode - erase previous contents if they exist

FileWrite($handle_dest,BinaryMid(FileRead($handle_src),$chunk_size+1,$count)) ; read the whole file with FileRead() and return chunked file with BinaryMid(), writing it to the new file with FileWrite()

; close files after using them
FileClose($handle_src)
FileClose($handle_dest)

 

Thanks for your help!

-Andrea

Link to comment
Share on other sites

9 hours ago, zgoro said:

I'm trying to figure out how to adapt this script to do the following:

1) Copy all .pvr files into the local folder to an /export folder

2) Remove 32 bytes in the header, and rename files into .bin

Try this :

#include <File.au3>
#include <FileConstants.au3>

Global $aFileArr, $sName, $sSourcePath, $sSourceExt, $sDestPath, $sDestExt
Global $hSourceFile, $hDestFile, $bFileString
Global $iChunkSize = 32 ; number of bytes to remove

$sSourcePath   = @ScriptDir & "\"
$sSourceExt    = ".pvr"
$sDestPath     = @ScriptDir & "\export\"
$sDestExt      = ".bin"

$aFileArr = _FileListToArray($sSourcePath, "*" & $sSourceExt, $FLTA_FILES)
For $i = 1 To UBound($aFileArr) -1 Step 1
    ; Open .pvr file :
    $hSourceFile = FileOpen($sSourcePath & $aFileArr[$i], $FO_BINARY)
    If @error Then
        MsgBox(4096, "Error :", "FileOpen -> SourcePath" & @CRLF)
        ExitLoop
    Else
        $bFileString = FileRead($hSourceFile)
        $sName = StringReplace($aFileArr[$i], $sSourceExt, "", -1)

        ; Create or overwrite .bin file in destination folder :
        $hDestFile = FileOpen($sDestPath & $sName & $sDestExt, BitOR($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY))
        If @error Then
            MsgBox(4096, "Error :", "FileOpen -> DestPath" & @CRLF)
            ExitLoop
        Else
            FileWrite($hDestFile, BinaryMid($bFileString, $iChunkSize + 1))
            FileClose($hDestFile)
        EndIf
        FileClose($hSourceFile)
    EndIf
Next

 

Probably not the shortest or most elegant way, but easy to follow (I hope ;)).

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Just a quick question Musashi, the script practically cuts 32 bytes from the beginning of the file but could it also cut specific offsets ranges into separate files?

For an instance:

File1 fm 0x00 to 0xD55
File2 fm 0XD56 to D5F
File3 fm ... to ....

Cheers! :)

 

Link to comment
Share on other sites

5 hours ago, zgoro said:

Just a quick question Musashi, the script practically cuts 32 bytes from the beginning of the file ...

Almost ;). Here is a simplified description of what happens (to the experts : please be indulgent) :

The .pvr file will be opened in binary mode (see FileOpen -> $FO_BINARY) ==>  "A file can be read as binary (byte) data by using FileOpen() with the binary flag."

Afterwards the read data will be assigned to a variable ($bFileString = FileRead($hSourceFile) ) by using FileRead . The naming $bFileString  may be a bit misleading, as it is bytes, not characters. Keep in mind, that every file is in principle just a sequence of bytes.

Finally, $bFileString will be written to a .bin file of the same name, leaving out the first 32 bytes (an existing .bin file will be overwritten).

6 hours ago, zgoro said:

... could it also cut specific offsets ranges into separate files?

To achieve this, you can modify the script. Please take a closer look at the function BinaryMid (it is not very fast but may be easier for you to follow). A bit of searching with google should also help you. There are already contributions that cover this.

Just create a script, experiment with it, and if you face problems, post it here for further assistance ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...