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
92 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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