File Management - MKDir

     

     

  • This statement enables to create Directory\Folder(s) as per user requirement
  •  

     

    (i)Create new Folder:

     

    Private Sub CommandButton1_Click()
    MkDir ("C:\CreateFolder")
    End Sub

     

  • MKDR represents to MAKE DIRECTORY
  • Create folder is FOLDER NAME
  •  

    Download The Workbook

     

    (ii)Create Sub Folders in a Folder:

     

    Private Sub CommandButton1_Click()

    MkDir ("C:\CreateFolder\Apple")
    MkDir ("C:\CreateFolder\Sriguranjani")
    MkDir ("C:\CreateFolder\Banana")
    MkDir ("C:\CreateFolder\Orange")
    MkDir ("C:\CreateFolder\Pen")
    End Sub

     

  • Create subfolders with names of APPLE,SRIGURANJANI, BANANA,ORANGE,PEN
  •  

    Download The Workbook

     

    (iii)Create Sub Folders in a Folder from pre-determined range

     

    Copy Code:

    Private Sub CommandButton1_Click()
    MkDir ("C:\ABC")
    Dim source As String, filename As String
    source = "c:\abc\"
    Dim max As Integer
    Max = Range(Range("A1"), Range("A1").End(xlDown)).Rows.Count
    For i = 1 To Max
    filename = Cells(i, 1).Value
    MkDir (source & filename)
    Next
    End Sub

     

  • MkDir
  • First create directory\folder with the name of ABC

     

  • Dim source As String, filename As String
  • Define variables as SOURCE & FILENAME to store the Folder and Subfolder names in looping process

     

  • source = "c:\abc\"
  • Assign source path to variable

     

  • Dim max As Integer
  • Define variable to store SUBFOLDERS data

     

  • For i = 1 To max
  • Assign values to FOR LOOP

     

  • filename = Cells(i, 1).Value
  • Assign value to FILENAME variable

     

  • MkDir (source & filename)
  • Create sub folders

     

    Download The Workbook

     

     

    (iv)Create Folder & Sub folder in different drives:

     

    Copy Code:

    Private Sub CommandButton1_Click()
    Dim dri As Integer
    dri = Range(Range("C15"), Range("C15").End(xlToRight)).Columns.Count
    c = 3
    For i = 1 To dri
    drname = Cells(15, c)
    MsgBox drname & ":\" & Range("D4").Value & "\"
    drive = drname & ":\" & Range("D4").Value & "\"
    'MkDir (drname & ":\" & "abc")
    MkDir (drive)
    Dim subfolder As String
    Dim max As Integer
    max = Range(Range("A1"), Range("A1").End(xlDown)).Rows.Count
    For h = 1 To max
    subfolder = Cells(h, 1).Value
    'MsgBox drive & filename
    MkDir (drive & subfolder)
    Next
    c = c + 1
    Next
    End Sub

     

     

  • Dim dri As Integer
  • Create variable DRI to store required drives list, and assign drives list

     

  • Mention the required Drives from C15 onwards
  • We can mention as per our requirement, as program created based dynamically

     

  • c = 3
  • As drives list started from 3rd column (C15 cells), created variable as "C" and assigned value as 3

     

  • For i = 1 To dri
  • drname = Cells(15, c)
  • Run the loop based on number of drives(DRI) and assign value to Drname

     

  • drive = drname & ":\" & Range("D4").Value & "\"
  • MkDir (drive)
  • Assigned Drive name whcih is mentioned in D4 cell

     

  • Dim subfolder As String
  • Dim max As Integer
  • max = Range(Range("A1"), Range("A1").End(xlDown)).Rows.Count
  • Create SUBFORLDER to store list of subfolders and create MAX varibale to run the loop which denotes maxmium value of loop

     

  • For h = 1 To max
  • Start the looping process to create sub folders

     

  • subfolder = Cells(h, 1).Value
  • Assign subfolders name which is mentioned in worksheet to SUBFOLDER variable

     

  • MkDir (drive & subfolder)
  • Denotes Main folder drive, name and subfolder name

     

  • Next
  • c = c + 1
  • Increase Column number from 3 to 4, which consists of DRIVE NAME

     

    Result: Folder and subfolders created in 3 drives C,D,E: