Sabtu, 31 Desember 2011

Copy, Cut, Rename

Kegiatan seperti Cut, Copy, Delete, dan Rename adalah kegiatan yang rutin dilakukan bila berhadapan dengan File maupun Folder. Kode-kode berikut ini akan menunjukkan bagaimana kegiatan tersebut dijalankan dalam VB. Kode ini menggunakan fungsi API yang lebih baik bila dibandingkan dengan fungsi bawaan / default.

Buat Module baru dan ketikkan :
[ VB 6.0 ]

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Long

Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
[ VB .NET ]
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

Structure SHFILEOPSTRUCT
Dim hWnd As Integer
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Integer
Dim fAborted As Boolean
Dim hNameMaps As Integer
Dim sProgress As String
End Structure


Lalu tambahkan kode berikut ini tepat setelah kode diatas.
Enum ffAction
FO_MOVE = &H1
FO_COPY = &H2
FO_DELETE = &H3
FO_RENAME = &H4
End Enum

Enum ffFlags
FOF_ALLOWUNDO = &H40 'menghapus ke recycle bin
FOF_NOCONFIRMATION = &H10 'menghilangkan pesan konfirmasi
FOF_NOCONFIRMMKDIR = &H200 'menghilangkan pesan pembuatan folder jika folder tidak ada
FOF_NOERRORUI = &H400 'menghilangkan pesan kesalahan
FOF_RENAMEONCOLLISION = &H8 'otomatis rename file jika ganda
FOF_SILENT = &H4 'menghilangkan dialog proses
End Enum

Public Sub FFOperation(ByVal Source As String, ByVal Destination As String, ByVal Action As ffAction, ByVal Flags As ffFlags)
Dim SO As SHFILEOPSTRUCT
With SO
.wFunc = Action
.pFrom = Source & Chr(0) & Chr(0)
.pTo = Destination
.fFlags = Flags
End With
SHFileOperation SO
End Sub



Berikut ini contoh penggunaannya :

1. CUT
VB 6.0 FFOperation "C:\Sumber.txt", "C:\Tujuan.txt", FO_MOVE, 0
VB .NET FFOperation("C:\Sumber.txt", "C:\Tujuan.txt", ffAction.FO_MOVE, 0)
Jika Anda ingin menghilangkan dialog proses-nya, ketikkan :
VB 6.0 FFOperation "C:\Sumber.txt", "C:\Tujuan.txt", FO_MOVE, FOF_SILENT
VB .NET FFOperation("C:\Sumber.txt", "C:\Tujuan.txt", ffAction.FO_COPY, ffFlags.FOF_SILENT)

2. COPY
VB 6.0 FFOperation "C:\Sumber.txt", "C:\Tujuan.txt", FO_COPY, 0
VB .NET FFOperation("C:\Sumber.txt", "C:\Tujuan.txt", ffAction.FO_MOVE, 0)
Jika Anda ingin menghilangkan dialog proses-nya, ketikkan :
VB 6.0 FFOperation "C:\Sumber.txt", "C:\Tujuan.txt", FO_COPY, FOF_SILENT
VB .NET FFOperation("C:\Sumber.txt", "C:\Tujuan.txt", ffAction.FO_COPY, ffFlags.FOF_SILENT)

3. DELETE
VB 6.0 FFOperation "C:\Sumber.txt", "", FO_DELETE, 0
VB .NET FFOperation("C:\Sumber.txt", "", ffAction.FO_DELETE, 0)
Jika Anda ingin menghilangkan dialog proses-nya, ketikkan :
VB 6.0 FFOperation "C:\Sumber.txt", "", FO_DELETE, FOF_SILENT
VB .NET FFOperation("C:\Sumber.txt", "", ffAction.FO_DELETE, ffFlags.FOF_SILENT)
Jika Anda ingin menghapus ke RecycleBin, ketikkan :
VB 6.0 FFOperation "C:\Sumber.txt", "", FO_DELETE, FOF_SILENT + FOF_ALLOWUNDO
VB .NET FFOperation("C:\Sumber.txt", "", ffAction.FO_DELETE, ffFlags.FOF_SILENT + ffFlags.FOF_ALLOWUNDO)

4. RENAME
VB 6.0 FFOperation "C:\NamaLama.txt", "C:\NamaBaru.txt", FO_RENAME, 0
VB .NET FFOperation("C:\NamaLama.txt", "C:\NamaBaru.txt", ffAction.FO_RENAME, 0)

Source : PutraVb

Artikel Terkait


EmoticonEmoticon