0

Hello world!

I've ran into a problem. I am getting directories contained in certain path and I need to separate the path VB.NET's giving me (like this: "D:\ApplicationFolder\Addons\Pack_1", "D:\ApplicationFolder\Addons\Pack_2" ...

Only into this: "Pack_1", "Pack_2"

So far I've tried this, but I can't get into a solution, I am lost...

Dim ADDONPACKS_DIRECTORIES As String() = Directory.GetDirectories(ADDONS_PATH) ' GETTING ALL DIRECTORIES (PATHS) IN THIS PATH

    For Each ADDONPACKS_DIRECTORY In ADDONPACKS_DIRECTORIES ' TRYING TO SPLIT FULL PATH OF THESE DIRECTORIES TO GET ONLY THE NAME OF THESE DIRECTORIES

        ADDONPACKS_DIRECTORY.Split()

        Dim ADDONPACKS_LENGTH As Integer = ADDONPACKS_DIRECTORY.Length()
        MsgBox(ADDONPACKS_DIRECTORY(2))

    Next

    ' Here I want to assign names of these directories onto a label. But the fields only show letters instead of the path segments.

    Addonpack1.Text = ADDONPACKS_DIRECTORIES(0)
    Addonpack2.Text = ADDONPACKS_DIRECTORIES(1)
    Addonpack3.Text = ADDONPACKS_DIRECTORIES(2)
    Addonpack4.Text = ADDONPACKS_DIRECTORIES(3)
    Addonpack5.Text = ADDONPACKS_DIRECTORIES(4)
    'Addonpack6.Text = ADDONPACKS_DIRECTORY(5)

Any ideas? I really appreciate further help.

benedikz
  • 27
  • 7
  • `ADDONPACKS_DIRECTORY(2)` You are asking for the third character in the string. – LarsTech Dec 03 '18 at 20:28
  • 2
    You probably just want `Dim s As String = Path.GetFileName(ADDONPACKS_DIRECTORY)` – LarsTech Dec 03 '18 at 20:31
  • There's no way this question wasn't asked before... yeah, i found some: https://stackoverflow.com/q/3736462, https://stackoverflow.com/q/5229292 , https://stackoverflow.com/q/3826763 – djv Dec 03 '18 at 21:42
  • 1
    Possible duplicate of [Get folder name from full file path](https://stackoverflow.com/questions/5229292/get-folder-name-from-full-file-path) – djv Dec 03 '18 at 21:51

1 Answers1

0

string.Split() is a Function: it returns a value.

Here: ADDONPACKS_DIRECTORY.Split(), you are splitting the string using the default separator (a white space) but the result is not assigned to anything, so it's lost (but it wouldn't be useful anyway).

This: MsgBox(ADDONPACKS_DIRECTORY(2)), will show only one char of the current Directory path. A string is a collection (an array) of chars. You're asking to show the 3rd.

If you think you won't need the complete directory listing anymore, you could Split the initial collection directly:

Dim ADDONPACKS_DIRECTORIES As String() = Directory.GetDirectories(ADDONS_PATH).
    Select(Function(d) d.Split("\"c).Last()).ToArray()

Addonpack1.Text = ADDONPACKS_DIRECTORIES(0)
'(...)

If you instead are going to use that collection of Paths later, you could Split each path and assign the result to each TextBox.Text property, leaving the original collection untouched:

Addonpack1.Text = ADDONPACKS_DIRECTORIES(0).Split("\"c).Last()
Addonpack2.Text = ADDONPACKS_DIRECTORIES(1).Split("\"c).Last()
'(...)

Do you know beforehand how many Addons you will have?
If not, a TextBox for each path might not be the right object to use as the output.
Maybe, you could use a single multiline TextBox. It's Lines() property will hold the array of all the Sub-Paths you appended.

Using the first snippet, it could be something like this:

For Each subpath As String In ADDONPACKS_DIRECTORIES
    TextBox1.AppendText(subpath & Environment.NewLine)
Next

Note:
As LarsTech noted in the comments, you could use Path.GetFileName() insted of splitting the path using the path separator.
It would work with both file names and path names, because Path.GetFileName returns the substring of a path when it first finds a path separator, parsing the string from the end to the start, no matter if the substring represents a path or a file name.

Addonpack1.Text = Path.GetFileName(ADDONPACKS_DIRECTORIES(0))
'(...)
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • 1
    `Path.GetFileName` does work in order to avoid all that split code. – LarsTech Dec 03 '18 at 20:44
  • @LarsTech Yes, that's true. But I think it's more important, here, to show the use of Split (because of the way the OP is using it). `Path.GetFileName` can be, in this case, confusing, since we're dealing with paths and not file names. I'll leave a note if you think it's useful. – Jimi Dec 03 '18 at 20:50