Solved

How to create folders using Visual Basic?

Posted on 2008-06-25
10
243 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

840 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