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
86 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 32

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 62

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 74

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 62

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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