0

Yesterday I learned to trim the file name off a file location using the flowing code

    Dim NEWPATH As String = (inventorApp.ActiveDocument.FullFileName)
    NEWPATH = NEWPATH.Substring(0, NEWPATH.fIndexOf("\"c))

This is realy neat because it is a more reliaable/ stable way to what I was using before... Haha today though i want the reverse I want to trim everything before the last slash how can i do that?

also just out of curiosity whats the lowercase c for in ("\"c) namely the code also works well without it?

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
TM80
  • 123
  • 2
  • 2
  • 12

3 Answers3

5

Rather than fiddling with substrings, you can use methods of the Path class:

Dim fullpath as String = inventorApp.ActiveDocument.FullFileName

'What you're after now - the filename
Dim justTheFileName as String = Path.GetFileName(fullpath)

'a replacement for what you're already doing to get the folder name
Dim justTheFolderName as String = Path.GetDirectoryName(fullpath)

The lowercase c in "\"c denotes that you want a Char rather than a String, which is what this particular overload of IndexOf takes, but there's a String overload too, so it would work equally well without the c.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • FYI, You need to either import System.IO in order to use the `Path` class or qualify the namespace in your code. MSDN: https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx .NET Source: http://referencesource.microsoft.com/#mscorlib/system/io/path.cs,090eca8621a248ee – вʀaᴎᴅᴏƞ вєнᴎєƞ May 04 '16 at 13:26
  • 1
    The VB IDE will offer to add the `Imports` statement provided there is a reference to the assembly (which is automatically the case for `System.IO`. – Ňɏssa Pøngjǣrdenlarp May 04 '16 at 15:27
  • 1
    @вʀaᴎᴅᴏƞвєнᴎєƞ Yes - and the same holds true for any class you use, of course. However, I don't feel that all answers that point out the existence of a class should need to go into the detail of how to reference, import and use classes. – James Thorpe May 04 '16 at 15:31
  • @Plutonix Thanks for the confirmation on that - I've been using ReSharper for so long I forget what's R# and what's VS... – James Thorpe May 04 '16 at 15:34
  • @JamesThorpe I know you know that the `Imports` statement is needed, I only added that comment for the sake of any future visitors whom may not know that and are not using VS like Plutonix mentioned. You got my +1 for being the correct answer. – вʀaᴎᴅᴏƞ вєнᴎєƞ May 04 '16 at 15:45
  • @вʀaᴎᴅᴏƞвєнᴎєƞ That's fine - but comments are transient, and could be removed at any time. I'm just saying why I won't be adding that info into my answer on the off chance the comments are removed :) – James Thorpe May 04 '16 at 15:54
0

You can use split() function or this

Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim x As Integer = InStr(mystr, cut_at)

Dim string_before As String = mystr.Substring(0, x - 2)
Dim string_after As String = mystr.Substring(x + cut_at.Length-1)
daniel aagentah
  • 1,672
  • 5
  • 29
  • 56
  • This will not work for the OP's needs. They are looking to trim all Directories off of a file path. Therefore the `cut_at` value will be ``\`` and unless the file is on the root of the drive, the `InStr()` function will return the position of the first ``\`` in the file path, not the last. At a minimum the `InStrRev()` function should be used instead. Secondly if this code is being used cross platform, ``\`` is only the directory separator in Windows, not Unix or Mac. – вʀaᴎᴅᴏƞ вєнᴎєƞ May 04 '16 at 15:04
0

A solution is to use LastIndexOf() to create a substring that will contain only what's after the last slashe :

Dim fullpath As String = inventorApp.ActiveDocument.FullFileName

Dim FileName As String = fullPath.Substring(fullPath.LastIndexOf("\"))

I like this one because it is more general (i.e. you can recover the file extension by replacing "\" by "."). However, if you know for sure that you are working with pathes to files, prefer James Thorpe's solution, using System.IO.Path

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • Or you could just use [`Path.GetExtension`](https://msdn.microsoft.com/en-us/library/system.io.path.getextension(v=vs.110).aspx). – James Thorpe May 04 '16 at 15:29