CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2001
    Location
    Medellín - Antioquia,Colombia - SurAmerica
    Posts
    86

    How to create a Directory

    Hello Everyone

    I need to create a directory, verify if exist first... Later to erase someone other directory...
    Please help me...

    [email protected]

    Please Response to:[email protected]
    http://www.bigfoot.com/~joseluisbz

  2. #2
    Join Date
    Sep 2000
    Posts
    40

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How to create a Directory

    ' this code will create the directory 'c:\Testing\Hello\How\Are\You\'

    Public Sub rMkDir(ByVal mdir As String)
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.GetParentFolderName(mdir) <> "" Then rMkDir FSO.GetParentFolderName(mdir)
    On Local Error Resume Next
    MkDir mdir
    End Sub

    Private Sub Form_Load()
    rMkDir "c:\Testing\Hello\How\Are\You\"
    End Sub

    '===another example===============================================================

    It uses the CreateDirectory API and the Dir() function to quickly create all the directories in the path.

    Declarations

    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


    Code

    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
    iFlag = 0
    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

    Use

    Call CreateNewDirectory("c:\test\directory\vb\tips\")


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How to create a Directory

    ' Delete this directory and all the files it contains.

    Private Sub DeleteDirectory(ByVal dir_name As String)
    Dim file_name As String
    Dim files As Collection
    Dim i As Integer

    ' Get a list of files it contains.
    Set files = New Collection
    file_name = Dir$(dir_name & "\*.*", vbReadOnly + vbHidden + vbSystem + vbDirectory)
    Do While Len(file_name) > 0
    If (file_name <> "..") And (file_name <> ".") Then
    files.Add dir_name & "\" & file_name
    End If
    file_name = Dir$()
    Loop

    ' Delete the files.
    For i = 1 To files.Count
    file_name = files(i)
    ' See if it is a directory.
    If GetAttr(file_name) And vbDirectory Then
    ' It is a directory. Delete it.
    DeleteDirectory file_name
    Else
    ' It's a file. Delete it.
    lblStatus.Caption = file_name
    lblStatus.Refresh
    SetAttr file_name, vbNormal
    Kill file_name
    End If
    Next i

    ' The directory is now empty. Delete it.
    lblStatus.Caption = dir_name
    lblStatus.Refresh
    RmDir dir_name
    End Sub

    Private Sub cmdDelete_Click()
    DeleteDirectory txtDir.Text
    lblStatus.Caption = "Files deleted."
    End Sub

    Private Sub Form_Load()
    Dim txt As String

    txt = App.Path
    If Right$(txt, 1) <> "\" Then txt = txt & "\"
    txt = txt & "Target"
    txtDir.Text = txt
    End Sub




    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured