Re-Join File


Option Explicit

Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const CREATE_ALWAYS = 2
Private Const OPEN_ALWAYS = 4
Private Const INVALID_HANDLE_VALUE = -1

Private Declare Function ReadFile Lib “kernel32” _
(ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) As Long

Private Declare Function CloseHandle Lib “kernel32” _
(ByVal hObject As Long) As Long

Private Declare Function WriteFile Lib “kernel32” _
(ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As Long

Private Declare Function CreateFile Lib _
“kernel32” Alias “CreateFileA” _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long

Private Declare Function FlushFileBuffers Lib “kernel32” _
(ByVal hFile As Long) As Long

Public Function JoinFiles(ByVal inputFilename As String) As _
Boolean

Dim fReadHandle As Long
Dim fWriteHandle As Long
Dim fSuccess As Long
Dim lBytesWritten As Long
Dim lBytesRead As Long
Dim ReadBuffer() As Byte
Dim TotalCount As Long
Dim Count As Integer
Dim FileName As String
Dim ret As Integer

‘ Kalau file output sudah ada
If Dir(inputFilename) <> “” Then
ret = MsgBox(“File Output (” & inputFilename & _
“) sudah ada.” & vbCrLf & _
“Akan ditindih??”, _
vbYesNo + vbQuestion, “Konfirmasi”)
If ret = vbNo Then

JoinFiles = False
Exit Function
Else
Kill inputFilename
End If
End If

Count = 1
FileName = Dir(inputFilename & “.1”)

‘No files to join
If FileName = “” Then
JoinFiles = False
Exit Function
End If

Do While FileName <> “”
Count = Count + 1
FileName = Dir(inputFilename & “.” & Count)
Loop
TotalCount = Count – 1

‘ Buka file handle untuk file yang hendak ditulisi
fWriteHandle = CreateFile(inputFilename, _
GENERIC_WRITE Or GENERIC_READ, 0, 0, _
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

‘ Jika sukses lanjutkan
If fWriteHandle <> INVALID_HANDLE_VALUE Then

For Count = 1 To TotalCount
‘ Buka file yang dibaca
ReDim ReadBuffer(0 To FileLen(inputFilename & “.” & Count))
fReadHandle = CreateFile(inputFilename & “.” & Count, _
GENERIC_WRITE Or GENERIC_READ, 0, 0, _
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

‘ Jika pembacaan sukses, lanjutkan
If fReadHandle <> INVALID_HANDLE_VALUE Then
‘ Baca blok pertama
fSuccess = ReadFile(fReadHandle, ReadBuffer(0), _
UBound(ReadBuffer), lBytesRead, 0)

‘ Tulis blok ke file
fSuccess = WriteFile(fWriteHandle, ReadBuffer(0), _
UBound(ReadBuffer), lBytesWritten, 0)

If fSuccess <> 0 Then
‘ Harus di-flush
fSuccess = FlushFileBuffers(fWriteHandle)
Else
‘ Jika ada error, keluar
JoinFiles = False
Exit Function
End If

fSuccess = CloseHandle(fReadHandle)

Else
‘ Jika ada error, keluar
JoinFiles = False
Exit Function
End If

Next Count
Else
‘ Jika ada error, keluar
JoinFiles = False
Exit Function
End If

‘ Tutup file setelah ditulis
fSuccess = CloseHandle(fWriteHandle)
JoinFiles = True

End Function

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdSplit_Click()
If JoinFiles(txtFileName.Text) Then
MsgBox “File telah berhasil di-gabung!”
Else
MsgBox “File gagal di-gabung!”
End If
End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s