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
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 34

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
Independent Software Vendors: 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!

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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