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
94 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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