Solved

How to create folders using Visual Basic?

Posted on 2008-06-25
10
242 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fso.FolderExists("\\server\HiddenFolder$") 4 68
Put window Form inside tab page 10 33
Point to Current Row Ater Refresh Datagridview 3 25
VS.net 2010 11 36
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

803 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