« Why you need the Migr… | Home | FileMaker 17 Launch P… »

Tip of the day: Split large files into smaller chunks

If you need to ever split files with MBS FileMaker Plugin, you can do it using the BinaryFile functions like the following script shows. It opens a file for reading and copies 10 MByte chunks to new files.

Set Variable [ $InputPath ; Value: "/Users/cs/Desktop/tets.mp4" ] 

Set Variable [ $ChunkSize ; Value: 10*1024*1024 ] 

# 

Set Variable [ $InputStream ; Value: MBS( "BinaryFile.Open"; $inputPath ) ] 

If [ MBS("IsError") ] 

Show Custom Dialog [ "Failed to open file." ; $InputStream & ¶ & $InputPath ] 

Exit Script [ Text Result:    ] 

End If

Set Variable [ $InputLength ; Value: MBS( "BinaryFile.Length"; $InputStream ) ] 

Set Variable [ $InputPosition ; Value: 0 ] 

Set Variable [ $Counter ; Value: 1 ] 

Loop

# Read chunk

Set Variable [ $Chunk ; Value: MBS( "BinaryFile.ReadHex"; $InputStream; $ChunkSize ) ] 

# 

# Write Chunk

Set Variable [ $OutputPath ; Value: $InputPath & "." & $Counter ] 

Set Variable [ $OutputStream ; Value: MBS( "BinaryFile.Create"; $OutputPath ) ] 

If [ MBS("IsError") ] 

Show Custom Dialog [ "Failed to create file." ; $InputStream & ¶ & $OutputPath ] 

Set Variable [ $r ; Value: MBS( "BinaryFile.Close"; $InputStream ) ] 

Exit Script [ Text Result:    ] 

End If

Set Variable [ $r ; Value: MBS( "BinaryFile.WriteHex"; $OutputStream; $Chunk ) ] 

Set Variable [ $r ; Value: MBS( "BinaryFile.Close"; $OutputStream ) ] 

# 

# next

Set Variable [ $InputPosition ; Value: $InputPosition + $ChunkSize ] 

Exit Loop If [ $InputPosition > $InputLength ] 

Set Variable [ $Counter ; Value: $Counter +1 ] 

End Loop

# 

# cleanup

Set Variable [ $r ; Value: MBS( "BinaryFile.Close"; $InputStream ) ] 

19 08 18 - 18:49