Results 1 to 6 of 6

Thread: MkDir, maybe an old one

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    6

    MkDir, maybe an old one

    Hi,

    I need to create a multilevel path like

    "C:\Level1\Level2\Level3"

    Public sub CreatePath()

    MkDir "C:\level1\level2\level3"

    End Sub

    leads to an error 76 "path not found".

    Public Sub createdir()

    MkDir "C:\level11"
    MkDir "C:\level11\level12"
    MkDir "C:\level11\level12\level13"

    End Sub

    works fine but isn't very practical when the path has a greater changing numbers of levels.

    Who knows how I can solve this problem on a simple way, suitable for a greater changing number of levels ??

    jodo1955

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: MkDir, maybe an old one

    Try this...

    I have not included error handling which I am sure you can take care of...

    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim StrFullPath As String, Myarray() As String
    3.    
    4.     '~~> Change this to the relevant path
    5.     StrFullPath = "C:\Level1\Level2\Level3"
    6.  
    7.     Myarray = Split(StrFullPath, "\")
    8.  
    9.     MyNewPath = Myarray(0)
    10.  
    11.     For i = 1 To UBound(Myarray)
    12.         '~~> Create Paths
    13.         MkDir MyNewPath & "\" & Myarray(i)
    14.         MyNewPath = MyNewPath & "\" & Myarray(i)
    15.     Next i
    16. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: MkDir, maybe an old one

    Use this API call:
    Code:
    Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
        ByVal lpPath As String) As Long
    Make sure lpPath ends with "\"
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MkDir, maybe an old one

    Quote Originally Posted by anhn View Post
    Make sure lpPath ends with "\"
    This is important...if you don't have the trailing "\" you will not receive an error or any other indication that it didn't work. However, without that "\" the folders will not be created.

  5. #5
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: MkDir, maybe an old one

    Example from APIGuide:

    Code:
    Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'create the directory 'c:\test\dir\hello\something\apiguide\'
        SHCreateDirectoryEx Me.hwnd, "c:\test\dir\hello\something\apiguide\", ByVal 0&
    End Sub

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: MkDir, maybe an old one

    Quote Originally Posted by Hack
    Quote Originally Posted by anhn
    Make sure lpPath ends with "\"
    This is important...if you don't have the trailing "\" you will not receive an error or any other indication that it didn't work. However, without that "\" the folders will not be created.
    That's good to be mentioned.
    I make a sub like this:
    Code:
    Sub MakeDeepDir(ByVal sPath As String)
       MakeSureDirectoryPathExists sPath & "\"
    End Sub
    You can pass in sPath with or without trailing "\" because it is accepted if the path ends with "\\".
    Unlike the classic MkDir, if the directory already existed, MakeSureDirectoryPathExists() will do nothing and not generate an error.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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