CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: directory

  1. #1
    Join Date
    Feb 2000
    Posts
    440

    directory

    OK, here is an easy question:
    How can I create a directory using Visual Basic.

    Thanks

    Valery Iskarov Nikolov
    Software Dynamics

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: directory

    if dir("c:\newDir", vbdirectory)= "" then
    mkdir "c:\newDir"
    end if

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: directory


    'Another way:
    'In general section
    private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (byval lpPathName as string, lpSecurityAttributes as SECURITY_ATTRIBUTES) as Long
    private Type SECURITY_ATTRIBUTES
    nLength as Long
    lpSecurityDescriptor as Long
    bInheritHandle as Long
    End Type

    private Sub Command1_Click()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim Security as SECURITY_ATTRIBUTES
    'Create a directory
    Ret& = CreateDirectory("C:\Directory", Security)
    'If CreateDirectory returns 0, the function has failed
    If Ret& = 0 then MsgBox "error : Couldn't create directory !", vbCritical + vbOKOnly
    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Feb 2000
    Posts
    440

    Re: directory



    10X

    Valery Iskarov Nikolov
    Software Dynamics

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

    Re: directory

    using FSO

    ' Inputs:sPath: Fully-qualified absolute or relative path you wish to create.
    'Example 1: \\NetworkVolume\NetworkShare\ExistingDir\NewDir\NewSubdir\NewSubDir\
    'Example 2: C:\Program Files\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z

    'Side Effects:Number 70, Permission Denied error will occur is write access or directory create access is
    ' not allowed for the drive for that user.

    'Reference "Microsoft Scripting Runtime" (scrrun.dll) available at http://www.microsoft.com/scripting or with VB6.

    Public Sub CreateFolders(ByVal sPath As String)
    Dim oFileSystem As New Scripting.FileSystemObject
    'or late-bind with:
    'Dim oFileSystem As Object
    'Set oFileSystem = CreateObject("Scripting.FileSystemObject")

    On Error Goto ErrorHandler
    With oFileSystem
    ' Is this drive valid and ready?
    If .DriveExists(.GetDriveName(sPath)) Then
    ' Is this folder not yet valid?
    If Not .FolderExists(sPath) Then
    ' Recurse back in to this method until a parent folder is valid.
    CreateFolders .GetParentFolderName(sPath)
    ' Create only a nonexistant folder before exiting the method.
    .CreateFolder sPath
    End If
    End If
    End With

    Set oFileSystem = Nothing
    ExitMethod:
    Exit Sub
    ErrorHandler:
    App.LogEvent "CreateFolders Error in " & Err.Source & _
    ": Could not create " & sPath & ".", vbLogEventTypeInformation
    End Sub




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

  6. #6
    Join Date
    Feb 2000
    Posts
    440

    Re: directory

    10x

    Valery Iskarov Nikolov
    Software Dynamics

  7. #7
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: directory

    Here is the simplest and most powerful way I found to create a Directory and a Directory tree. You do not need to worry about whether the parent exists or not. One statewment will create a multi-level folder tree if that is what you need.
    The little sample creates a directory tree 5 levels deep.
    [vbcode]
    Option Explicit

    Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
    Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'create the directory "c:\this\is\a\test\directory\", if it doesn't exist already
    MakeSureDirectoryPathExists "c:\this\is\a\test\directory\"
    End Sub

    [/vbcode

    John G

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