Exact Folder name in an Array


#1

Hello All,

I am trying to have an array of folders from a directory without the full file path. I want this so I can have a list of these folders as options for the input dialog module. I have figured I can get an array of the variables using directory.getdirectories(“path”) but cannot figure out how to remove the file path from each of the strings in my array.


#2

Hi @travis2118

Please refer the following link. You would get the solution for the problem.

Cheers.


#3

Thank you for your response, but I need all of the file names stored in an array variable, not displayed individually in message boxes.


#4

Unfortunately that solution does not work for my specific problem as the message box does not allow be to update my array variable.

Best,
Travis


#5

Hi @travis2118

If I were to do this, I would take your array of filepaths and just use a .Select on it to change the values and create an array where it is just the filenames or foldernames

directory.getdirectories("path").Select(Function(f) path.getfilename(f) )

Regards.


#7

Hello @ClaytonM

Thank you for your help. Can you explain what variable f is here? My directory.getdirectories(“path”) successfully gets me an array of folders within that path. Now I just need help on using Select.

Thanks!


#8

It is a lambda expression with vb.net syntax
So basically, when you have a list, array, or enumerable, you can add a period after it and it will list many functions that can be used with the list.

.Where() and .Select() are probably the most used, because .Where() let’s you pull in only certain values and .Select() let’s you massage or manipulate those values any way you would like. The result is always another list or array.

So going back to my example.
You get an array of filepaths by using directory.getdirectories("path") —you can store this into another variable or just manipulate the array in one line like I have done for you.

When you add the .Select(), it essentially is going to go through each item in the array and perform the action on the value to get a new value for the array. Function(f) basically initializes the variable to be used and only used within the parenthesis of the function used (ie Select() or Where())… so “f” is a variable that represents each item within the array you are going through, and you can use any letter or word as the variable here.

So, when I place “f” inside the getfilename(), it is taking each filepath from the .getdirectories() array and pulling out only the filename or foldername to create a new array with the new values.

You can also take this further, given certain requirements, and filter the array:

directory.getdirectories("path").Select(Function(f) path.getfilename(f) ).Where(Function(f) f.StartsWith("abc") )

Which would return all foldernames that start with “abc”. So, there’s a lot you can do with this.

I hope my explanation was good enough for you to understand how it works.

In summary,
.Select(Function(variable) getfilename(variable) ) will go through each item in the list and change the value to only be the filename, which returns the desired array. And, remember that the variables used inside Function() does not need to be declared in variables and is only used within the parenthesis of that function, which means it can be used again if you use multiple functions in one line (but don’t use a variable name that is also declared in variables or there could be some conflicts.

Regards.


#9

Thank you @ClaytonM that makes a lot of sense!

Here is the error message I get when I try and follow your instructions:

"Compiler error(s) encountered processing expression
‘Directory.GetDirectories(“C:\Billing”).Select(Function(f) path.getfilename(f)’ .
Option Strict On disallows implicit conversions from
‘System.Collections.Generic.IEnumerable(Of String) to 1-dimensional array of string’

Any idea whats going wrong?


#10

Yeah,
Anytime you see “disallows converstions from… to …” it means you are trying to assign one type to a different type; both sides in the Assignment should be the same type. when you use any function that returns a list like that it is usually of type IEnumerable, so you can simply use .ToArray on the end to resolve the issue since you are assigning it to an Array type variable.

Directory.GetDirectories(“C:\Billing”).Select(Function(f) path.getfilename(f) ).ToArray

Regards.


#11

@ClaytonM

You rock! Thanks for all your help and explaining the steps to me!