Click here to Skip to main content
Licence CPOL
First Posted 7 Apr 2010
Views 8,302
Downloads 0
Bookmarked 5 times

Create Bulk or Mass Folders using Names from Excel Spreadsheet with Visual Basic.NET

By | 7 Apr 2010 | Article
This sample code tells you how to create a series of folders using folder names stored in an Excel spreadsheet.

Introduction

This sample code tells you how to create a series of folders using folder names stored in an Excel spreadsheet.

How to Use

  • Open any Excel spreadsheet which contains folder names. You can get a sample spreadsheet from downloaded source.

  • Select folder names and copy them from this Excel spreadsheet.
  • Open downloaded source code with Visual Studio 2005 and run it. The following form should appear.

  • Click on Paste from Excel. This should paste values from Excel spreadsheet into DatagridView. If it does not paste, make sure you have copied values from Excel spreadsheet.

  • Enter path of directory where you want to create folders and make sure it exists.
  • Click Create Folders button. This will create folders in specified directory.

How It Works

There are two main functionalities used in this sample:

  1. Get directory name values from Excel spreadsheet using Clipboard object.
  2. Create folders using DirectoryInfo object.

(1) Get directory name values from Excel spreadsheet

VB.NET clipboard object allows you to get text from Clipboard object. Here is the code:

Dim s As String = Clipboard.GetText()
'When you get excel clipboard data as string, it will separate 
'cells with a new line character. Use this new line character 
'to split values in to an array.
Dim cells() As String = s.Split(vbNewLine)
'Now Cells() array will hold all directory names.

Use cells() array to build a datatable and assign it to DataGridView as datasource.

'Create data table with directory names.
Dim DT As New DataTable()
DT.Columns.Add("Directory Name")
Dim i As Integer
'Loop through cells() array and add rows in data table.
For i = 0 To cells.Length - 1
    DT.Rows.Add(New Object() {cells(i)})
Next
DataGridView1.DataSource = DT

(2) Create Folders

Once you have folder names in DataGridView, you can use it to create folders with DirectoryInfo() object.

'Get values from DataGridView datasource to a DataTable.
Dim DT As DataTable = DataGridView1.DataSource

Dim i As Integer
'Loop through grid and get directory names.
For i = 0 To DT.Rows.Count - 1
    'Get directory name.
    Dim DirName As String = DT.Rows(i)("Directory Name").ToString()
    'Trim directory name to remove spaces from beginning or end of string.
    DirName = Trim(DirName)
    If (DirName  "") Then
        'Initialize a DirectoryInfo object with this directory.
        'txtDir is the path where directories needs to be created.
        Dim oDir As New DirectoryInfo(txtDir.Text + DirName) 
        oDir.Create()
    End If
Next

History

  • 7th April, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shabdar Ghata

Web Developer

Canada Canada

Member

Software Developer
 
http://www.shabdar.org

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAppreciate this Pinmemberdanteellis18:55 28 Mar '11  
GeneralBut why use....... Pinmemberdaveauld20:19 10 Apr '10  
QuestionPractical Use? Pinmemberchrixko10:32 7 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 Apr 2010
Article Copyright 2010 by Shabdar Ghata
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid