Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there an easy way to strip, or rather get the filename from a string. The string contains the directory path of the file. In addition remove the extension.

    Example c:/filefolder/test.doc

    I am trying to put test in a variable.

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Unlike Unix and Mac OS, Windows uses backslashes in file paths, so it would be c:\filefolder\test.doc

    You could use code like this:

    Dim strPath As String
    Dim strFile As String
    Dim intPos1 As Integer
    Dim intPos2 As Integer

    strPath = "c:\filefolder\test.doc"
    intPos1 = InStrRev(strPath, ".")
    intPos2 = InStrRev(strPath, "\")
    strFile = Mid(strPath, intPos2 + 1, intPos1 - intPos2 - 1)

  3. #3
    Lounger
    Join Date
    Mar 2009
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    f = "c:\filefolder\test.doc"
    fname = Split(f, "\")(UBound(Split(f, "\")))

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    That's a neat way to get the complete filename.

    To remove the extension, as requested by the original poster, one could use

    Dim f, fname, a, i
    f = "c:\filefolder\test.doc"
    a = Split(f, "\")
    fname = a(UBound(a))
    i = InStrRev(fname, ".")
    fname = Left(fname, i - 1)

    (Since it's allowed to use multiple periods in a filename, we can't use Split(fname, ".") to get the filename without the extension.)

  5. #5
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks to you both. I agree with you Hans, our friend came up with a short way to split the string. I ended up using:

    fname = Left(fname, Len(fname) - 4) to get me extension removed. It seemed to work, just hope it won't let me down later.

    Best regards and thanks again.

  6. #6
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    [quote name='ZOR' post='771153' date='18-Apr-2009 17:56']Many thanks to you both. I agree with you Hans, our friend came up with a short way to split the string. I ended up using:

    fname = Left(fname, Len(fname) - 4) to get me extension removed. It seemed to work, just hope it won't let me down later.

    Best regards and thanks again.[/quote]

    You can't assume that extensions are always 3 characters long. Files created with Office 2007 can have a 4 or 5 character extension, and html files are 4.
    To find the length of the extension you could use InstRev to find the position of the last . from the end.
    Regards
    John



  7. #7
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks John. I will use Hans code then, and know I'm safe. Have a good weekend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •