Solved

How to create folders using Visual Basic?

Posted on 2008-06-25
10
246 Views
Last Modified: 2012-05-05
I am using Folder Browser Dialog ... I would like to create bunch of folders under

FolderBrowserDialog.selectedpath

How is that possible? which shell command I can use?
0
Comment
Question by:CalmSoul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 21867535
Hi

You got some useful tools in the System.IO namespace, import System.IO and use for instance what's in the attached code snippet.

/Carl.
Imports System.IO
 
Dim NewDirectory as New DirectoryInfo("Your path and directory name, i.e. C:\MyNewDirectory")
NewDirectory.Create()

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21867589
I am trying this but now working... I want to make folder called "temp"

How to make and name folder at same time?
     Dim NewDirectory As New DirectoryInfo(" & Me.FolderBrowserDialog1.SelectedPath & ")
        NewDirectory.Create()

Open in new window

0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21867641
Dim BasePath As String = Me.FolderBrowserDialog1.SelectedPath
IO.Directory.Create(BasePath & "\temp")
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:carlnorrbom
ID: 21867685
Like jcoehoorn states in his example you need to append the new directory name to the selected path before calling the create() method, i.e:

Dim NewDirectory As New DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath.ToString + "\temp")
NewDirectory.Create()
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21867753
Thanks carlnorrbom:

How can I put validation that only create new if it doesn't exist otherwise do somethign else
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 250 total points
ID: 21868179
CalmSoul,

Try this:

Dim NewDirectory As New DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath.ToString + "\temp")
If Not NewDirectory.Exists Then
   NewDirectory.Create()
Else
   'Other logic here
End If

0
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 250 total points
ID: 21868296
That will throw an exception trying to create the DirectoryInfo object if the directly does not exist.
Dim BaseFolder As String = Me.FolderBrowserDialog1.SelectedPath
Dim NewFolder As String = BaseFolder & "\temp"
If IO.Directory.Exists(NewFolder) Then
    IO.Directory.Create(NewFolder)
Else
    'Do something else
End If

Open in new window

0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 21868587
jcoehoorn,

What do you base that on? Just ran it without any exceptions being thrown? I am using the "New" keyword in order to dimension up a new object which does NOT exist yet as we are about to create it?

It would surely throw an exception if it was used in the manner:
Dim NewDirectory as DirectoryInfo("Invalid path as string")

Or am I mistaking?

/Carl.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21868724
I ran a check myself and I stand corrected- it does allow you to create instances for non-existent directories.  I could have sworn I've had code that had an exception there before...
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 21868923
jcoehoorn,

I think what's key here and what's missing from both proposed solutions is validation on the path, I mean there is no need to validate the object that is supposed to be created. When using the FolderBrowserDialog I guess the selected path will always be valid unless cancelled? Maybe just a:

If Not FolderBrowserDialog1.SelectedPath Is Nothing Then
   'Logic..
End If

Anyhow, CalmSoul, good luck with your solution!
/Carl.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question