Alex
Alex

Reputation: 112

VB.NET Retrieve File path of File in Parent directory without parent Directory path included

So I have a directory where i want to get for all files in all including subdirs, the path of them relative to the parent directory. Example:

Parent directory: "C:\test"
File1 located in: "C:\test\foo\bar.txt"

what i want to retrieve is:

"foo\bar.txt"

I know how to do it with

For Each foundFile As String In My.Computer.FileSystem.GetFiles(Path)

but that will return the full Path to me.

Upvotes: 1

Views: 466

Answers (1)

FloatingKiwi
FloatingKiwi

Reputation: 4506

So you have:

    Dim root = "c:\temp\"
    Dim files = My.Computer.FileSystem.GetFiles(root, FileIO.SearchOption.SearchAllSubDirectories)

Now just take the substring starting at the length of the root directory.

    Dim filesWithoutRoot = files.Select(Function(f) f.Substring(root.Length)).ToList

Upvotes: 1

Related Questions