CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2008
    Location
    Pakistan
    Posts
    41

    Question saprit name in strings

    dear friend
    i have a string like
    "d:\proj\pics\ds0001" this is not a static strings
    some time drive latter may be change and some time fowlder in dowlder changed like "c:\document and setting\all user\dasktop\DS0008"
    or "f:\my fowlder\your\me\tum\vehicle\my pic"

    i want to get last strings or called file name (ds0001,DS0008,my pic) in above strings on runtime

    thanks

  2. #2
    Join Date
    May 2008
    Posts
    224

    Re: saprit name in strings

    Here is one method of parsing a filename from a path
    Code:
    Private Sub Command1_Click()
    Dim FileName As String
    Dim Temp() As String
    Dim x As Integer
    Dim PathName As String
    	PathName = "C:\temp\pictures\jpegs\Mypic.jpg"
    	Temp = Split(PathName, "\")
    	x = UBound(Temp)
    	FileName = Temp(x)
    	MsgBox FileName
    End Sub

  3. #3
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: saprit name in strings

    It is better to use FileSystemObject.
    Here is one example:

    Code:
        Dim fso As Object
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        'This will return 'DS0008.jpg'
        MsgBox fso.GetFileName("c:\document and setting\all user\dasktop\DS0008.jpg")
        
        'This will return 'DS0008'
        MsgBox fso.GetBaseName("c:\document and setting\all user\dasktop\DS0008.jpg")
        
        'This will return 'jpg'
        MsgBox fso.GetExtensionName("c:\document and setting\all user\dasktop\DS0008.jpg")

  4. #4
    Join Date
    May 2008
    Posts
    224

    Re: saprit name in strings

    Quote Originally Posted by Shaikh.Riyaz.a
    It is better to use FileSystemObject.
    Better? I wouldn't go so far as to say that. Either will work fine in this case. FSO likely takes more memory, works fine with filenames. Split works with strings so is not limited to file names. Both seem to run at about the same speed.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: saprit name in strings

    takes another reference that isn't needed for speed
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: saprit name in strings

    Two more methods to retrieve the filename from the full path would be:
    Code:
    Private Function GetFileName(FullPath As String) As String
    GetFileName = Mid$(FullPath, InStrRev(FullPath, "\") + 1)
    End Function
    Or via API:
    http://allapi.mentalis.org/apilist/GetFileTitle.shtml
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured