Cara Membuat Dan Menghapus Folder Dengan VB

    Share

    eRGe@N5
    Member Newbie
    Member Newbie

    Male
    Thread & Post : 14
    Point Forum : 207033
    Thanks Given : 0
    Join date : 02.08.11
    Age : 27
    Lokasi : N3 & N5

    default Cara Membuat Dan Menghapus Folder Dengan VB

    Post by eRGe@N5 on Thu Aug 18, 2011 2:06 am

    Ketik Coding Berikut ini pada Module Project




    Spoiler:
    Option Explicit

    Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type
    Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

    'Fungsi mencek keberadaan folder
    Public Function DirectoryExist(DirPath As String) As Boolean
    DirectoryExist = Dir(DirPath, vbDirectory) <> ""
    End Function

    'Fungsi untuk membuat Folder
    Public Sub CreateNewDirectory(NewDirectory As String)
    Dim sDirTest As String
    Dim SecAttrib As SECURITY_ATTRIBUTES
    Dim bSuccess As Boolean
    Dim sPath As String
    Dim iCounter As Integer
    Dim sTempDir As String

    sPath = NewDirectory

    If Right(sPath, Len(sPath)) <> "\" Then
    sPath = sPath & "\"
    End If

    iCounter = 1

    Do Until InStr(iCounter, sPath, "\") = 0
    iCounter = InStr(iCounter, sPath, "\")
    sTempDir = Left(sPath, iCounter)
    sDirTest = Dir(sTempDir)
    iCounter = iCounter + 1
    'create directory
    SecAttrib.lpSecurityDescriptor = &O0
    SecAttrib.bInheritHandle = False
    SecAttrib.nLength = Len(SecAttrib)
    bSuccess = CreateDirectory(sTempDir, SecAttrib)
    Loop
    End Sub

    'Fungsi Untuk Menghapus folder
    Public Sub DelDirectory(sName as String)
    On Error Resume Next
    Dim Fso
    Set Fso = CreateObject("Scripting.FileSystemObject")
    If Dir(sName, vbDirectory) <> "" Then
    Fso.DeleteFolder sName
    End If
    Set Fso = Nothing
    End Sub


    Nah sekarang tinggal menggunakan fungsi-fungsi tersebut kedalam Form Project sebagai contoh membuat sebuah folder di drive C dengan nama Database coding nya seperti dibawah ini


    Spoiler:
    'Coding untuk mebuat Folder
    'Cek dulu Folder
    If DirectoryExist("C:\Database") <> True Then
    'Jika Folder yg dimaksud belum ada maka akan dibuat
    Call CreateNewDirectory("C:\Database\")

    'Coding Menghapus Folder
    'Kalau Folder yang dimaksud ditemukan langsung dihapus
    Call DelDirectory ("C:\Database\")


      Waktu sekarang Thu Dec 08, 2016 10:48 am