Solved

How to create folders using Visual Basic?

Posted on 2008-06-25
10
238 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now