extracting file name from path.-VBForums
Results 1 to 13 of 13

Thread: extracting file name from path.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    extracting file name from path.

    Hi, I used the openfiledialog to select a file and managed to get the full path to the selected file using openfiledialog.fileName() but now i am trying to extract only the file name. E.g C:\temp\hello.mp3 i want to extract the "Hello.mp3" only. How can it be done?


    thanks alot!

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: extracting file name from path.

    VB Code:
    1. Dim s As String = System.IO.Path.GetFileName(path)
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: extracting file name from path.

    VB Code:
    1. Dim s As String = Microsoft.VisualBasic.Right(path, path.Length - path.LastIndexOf("\") - 1)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: extracting file name from path.

    Hi hyper88,

    you can use this Code to get the file name...

    VB Code:
    1. Dim FullPath, FileName As String
    2. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    3.             FullPath = OpenFileDialog1.FileName
    4.             MessageBox.Show(FullPath)
    5.             FileName = FullPath.Substring(FullPath.LastIndexOf("\") + 1)
    6.             MessageBox.Show(FileName)
    7. End If

    Hope this helps...

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,245

    Re: extracting file name from path.

    Why would you use an alternative when there is a method called GetFileName that is thus self-documenting and is also neater code?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: extracting file name from path.

    Quote Originally Posted by jmcilhinney
    Why would you use an alternative when there is a method called GetFileName that is thus self-documenting and is also neater code?
    Because it's safer to do string manipulation than using IO channels
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,669

    Re: extracting file name from path.

    Quote Originally Posted by ComputerJy
    Because it's safer to do string manipulation than using IO channels
    Thats both meaningless and stupid. GetFileName uses no "IO channels" whatever you imagine those to be, it just does string manipulation.

    jmc is right.
    I don't live here any more.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: extracting file name from path.

    IO.Path.GetFileName is plain string manipulation. And it is safer as it tests for invalid characters and invalid path length...

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,245

    Re: extracting file name from path.

    As stated above. Here's the actual implementation:
    VB Code:
    1. Public Shared Function GetFileName(ByVal path As String) As String
    2.       If (Not path Is Nothing) Then
    3.             Path.CheckInvalidPathChars(path)
    4.             Dim num1 As Integer = path.Length
    5.             Dim num2 As Integer = num1
    6.             Do While (--num2 >= 0)
    7.                   Dim ch1 As Char = path.Chars(num2)
    8.                   If (((ch1 = Path.DirectorySeparatorChar) OrElse (ch1 = Path.AltDirectorySeparatorChar)) OrElse (ch1 = Path.VolumeSeparatorChar)) Then
    9.                         Return path.Substring((num2 + 1), ((num1 - num2) - 1))
    10.                   End If
    11.             Loop
    12.       End If
    13.       Return path
    14. End Function

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: extracting file name from path.

    Quote Originally Posted by wossname
    Thats both meaningless and stupid. GetFileName uses no "IO channels" whatever you imagine those to be, it just does string manipulation.

    jmc is right.
    Sorry, I didn't know that. but thank you for your polite words anyway
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  11. #11
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: extracting file name from path.

    Quote Originally Posted by jmcilhinney
    As stated above. Here's the actual implementation:
    VB Code:
    1. Public Shared Function GetFileName(ByVal path As String) As String
    2.       If (Not path Is Nothing) Then
    3.             Path.CheckInvalidPathChars(path)
    4.             Dim num1 As Integer = path.Length
    5.             Dim num2 As Integer = num1
    6.             Do While (--num2 >= 0)
    7.                   Dim ch1 As Char = path.Chars(num2)
    8.                   If (((ch1 = Path.DirectorySeparatorChar) OrElse (ch1 = Path.AltDirectorySeparatorChar)) OrElse (ch1 = Path.VolumeSeparatorChar)) Then
    9.                         Return path.Substring((num2 + 1), ((num1 - num2) - 1))
    10.                   End If
    11.             Loop
    12.       End If
    13.       Return path
    14. End Function
    Dear jmcilhinney,

    I tried to test your code but it’s given an error on (CheckInvalidPathChars, DirectorySeparatorChar, AltDirectorySeparatorChar, VolumeSeparatorChar)
    saying “not member of String”??? any idea?

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  12. #12
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: extracting file name from path.

    Quote Originally Posted by phenom
    Dear jmcilhinney,

    I tried to test your code but it’s given an error on (CheckInvalidPathChars, DirectorySeparatorChar, AltDirectorySeparatorChar, VolumeSeparatorChar)
    saying “not member of String”??? any idea?

    Regards,

    he was just posting how the io.path.getfilename function works(because someone didnt think it was string minipulation).

    so dont add that function and just use

    dim name as string = io.path.getfilename(path)

  13. #13
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: extracting file name from path.

    Dear high6,

    thanks for the Info...

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.