Solved

How do you create folders from a list of names if does not already exist using VB.NEt?

Posted on 2014-11-17
6
93 Views
Last Modified: 2016-02-18
Hi,


If S = "MasterNSN, MasterNSC, MasterUFC, etc...)

How do I create a folder for each name in my application's folder at runtime?

Thanks,

Victor
0
Comment
Question by:vcharles
6 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40447538
If this is how your list is truly formatted, this this is one way you could do it (assuming that your list is a string):
Imports System.IO
Imports System.Windows.Forms

Module Module1
	Sub Main()
		Dim s As String = "MasterMSN, MasterNSC, MasterUFC"
		For Each folder As String In s.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
			Dim directory As New DirectoryInfo(String.Format("{0}\{1}", New FileInfo(Application.ExecutablePath).DirectoryName, folder.Trim()))
			If Not directory.Exists Then
				directory.Create()
			End If
		Next
	End Sub
End Module

Open in new window


-saige-
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40447543
Hi Victor;

Try it like this.

'' List of directories to be created if does not exist
Dim S As New List(Of String) From {"MasterNSN", "MasterNSC", "MasterUFC"}
'' Directory in which to check and create the new directories
Dim directory As String = "C:\Working Directory\Temp\"

'' Go through the list one by one
For Each folderName As String In S
    '' Check to see if the direcoty exist if not create it
    If Not Exists(directory & folderName) Then
        My.Computer.FileSystem.CreateDirectory(directory & folderName)
    End If
Next

Open in new window

Imports System.IO.File
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40447555
Or just for giggles, the LINQ-y approach:

Dim S = "MasterNSN, MasterNSC, MasterUFC"

S.Split(","c) _
 .Select(Function (name) name.Trim()) _
 .Where(Function (name) Not Directory.Exists(name)) _
 .ToList() _
 .ForEach(Function (name) Directory.CreateDirectory(name))

Open in new window


If you ensure that your strings don't have extra spaces in them, then you can ditch the Select/Trim line. If you happen to be using the morelinq library that Jon Skeet helped write, then you could ditch the ToList call.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:vcharles
ID: 40447578
Thank you for the solutions, will get back to you.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 40451785
Thank You for the solutions.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40452054
Not a problem, glad to help.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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