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:
- Get directory name values from Excel spreadsheet using
Clipboard
object.
- 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()
Dim cells() As String = s.Split(vbNewLine)
Use cells()
array to build a datatable
and assign it to DataGridView
as datasource
.
Dim DT As New DataTable()
DT.Columns.Add("Directory Name")
Dim i As Integer
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.
Dim DT As DataTable = DataGridView1.DataSource
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
Dim DirName As String = DT.Rows(i)("Directory Name").ToString()
DirName = Trim(DirName)
If (DirName "") Then
Dim oDir As New DirectoryInfo(txtDir.Text + DirName)
oDir.Create()
End If
Next
History
- 7th April, 2010: Initial post