-1

I know that IO.Directory.GetDirectories("X:/mydata/backup/stuff", "*", IO.SearchOption.AllDirectories) works for getting all directories, but I need to figure out how to not have the parent half attached to each string.

I need the output to be:

  • stuff/that/was/backed/up/file0.ext
  • stuff/that/was/backed/up/file1.ext
  • stuff/that/was/backed/up/file3.ext

Instead of:

  • X:/mydata/backup/stuff/that/was/backed/up/file0
  • etc.

The path can be anything, so I can't use a hard-coded solution either, like using Split(path, "/", 2) in the Application.StartupPath directory (which I currently use).

  • You didn't show what you mean by the parent half, but perhaps you are looking for `theFullPath.Replace(parentHalf, "")`. – Andrew Morton Dec 02 '18 at 20:33
  • I edited your question to make it easier to understand. Feel free to edit it further where you think is needed. One more thing, you do realize that `GetDirectories()` only returns the sub-directories and does **Not** include the files, right? Because you're using file paths in your output examples. If you need the files, you can use `GetFiles()` and if you need both the sub-directories and the files, you can use [`GetFileSystemEntries()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfilesystementries) instead. – 41686d6564 stands w. Palestine Dec 02 '18 at 20:48
  • I know that, I just used GetDirectories() in my example – Ace of code Dec 02 '18 at 21:05
  • Do you always want the last directory of "X:\mydata\backup\stuff" to be included in the "stuff\that\was\backed\up\file0.ext"? If so, what if there is no last directory, e.g. "X:\"? – Andrew Morton Dec 03 '18 at 09:37

2 Answers2

0

You can use the methods in System.IO.Path to manipulate path strings.

To separate directory and filename:

Dim filenameAndExtension As String = Path.GetFileName(fullPath)
Dim directory As String = Path.GetDirectoryName(fullPath)

You can also use Path.GetFileName(directory) to get the last part of a directory path.

To combine directory and filename:

Dim fullPath As String = Path.Combine(directory, filenameAndExtension)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • but I need to include the path from the selected folder to the file – Ace of code Dec 02 '18 at 20:15
  • I don't need the full path. If the selected folder is "foldername" and subfolders and files within were named "folder0, folder1, file0, file1" and there are files named "file2" and "file3" in the subfolder, I need to find the path from the selected folder to each subfolder and file. i need "Path/to/file/or/folder/filename.txt", not "D:/data/stuff/path/to/file/or/folder/filename.txt" or "filename.txt". – Ace of code Dec 02 '18 at 20:31
  • If you want a relative path, see: [Getting path relative to the current working directory?](https://stackoverflow.com/questions/703281/getting-path-relative-to-the-current-working-directory). – Olivier Jacot-Descombes Dec 02 '18 at 20:39
  • I will post my working code, hopefully you can make heads or tails out of it – Ace of code Dec 02 '18 at 20:41
  • google link to current code: https://drive.google.com/open?id=1GweGr4MUCTjOTEmVhU2o1VMWMyYjnmiB – Ace of code Dec 02 '18 at 20:45
  • I plan on changing everything to an xml based system that encodes data to Base64 strings. the first issue is the fact that if a user chooses a folder other than the hard coded folder in the application directory, it glitches out. – Ace of code Dec 02 '18 at 20:46
  • This seems to be a conceptual problem. Not a programming problem. Store the folder in a settings file an ini file or ask for it in the application and store it in the registry. – Olivier Jacot-Descombes Dec 03 '18 at 14:30
  • I created my own solution. It was programming, not conceptual. – Ace of code Dec 05 '18 at 18:58
0

after much hair pulling and completely unhelpful comments, I have found a solution:

Dim unitstring As String() = Split(TextBox2.Text, "\") 'separate directory names
Dim unitcount As Integer = Array.LastIndexOf(unitstring, Path.GetFileName(TextBox2.Text)) + 2 'get how far in the directory we are from the root. 
Dim paths As New List(Of String) 'list to hold results
For Each x In IO.Directory.GetDirectories(TextBox2.Text, "*", SearchOption.AllDirectories) 'loop through all directories to get them in the format needed
    Dim pathsplit As String() = Split(x, "\", unitcount) 'split from parent
    paths.Add(pathsplit(unitcount - 1)) 'the last item in the array is what is needed.
Next

if anyone else needs code like this, feel free to use it.