Solved

How to create folders using Visual Basic?

Posted on 2008-06-25
10
247 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

624 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