Results 1 to 2 of 2

Thread: Way to get the filename with dir

  1. #1
    Guest
    Okay, not asking anything, just giving a tip.

    If you're using dir to get filename in a directory, but don't want to get the path in the filename, you can try following:

    Code:
    'we suppoce Directory is a string that contains the directory we want to look for
    'add listbox and modify this as you like or make it a function
        Dim Filename As String
        If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
        Filename = Dir(Directory & "*.*", 0)
        Do While Filename <> ""
            Filename = Right(Filename, Len(Filename) - Len(Directory)
            Select Case Filename
                 Case Not ".", Not ".."
                     List1.AddItem Filename
            End Select
            Filename = Dir
        Loop
    I'm just get bored for some people asking all the time on how to do this
    Hope they're wise enough to pop-up in here today

    Add your own tips here if you know usually asked one and if you like to

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Hi. There are also several useful methods of the FileSystemObject that can extract parts out of a path. Instead of writing a loop to pull out the name or extension, just try the following.

    Code:
    Private Sub Command1_Click()
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
     
    'To get the file extension from a path
    MsgBox fs.GetExtensionName("c:/extension.txt") ' returns txt
    
    'To get the filename only from a path, no extension.
    MsgBox fs.GetBaseName("c:/extension.txt") ' returns extension 
    
    'To get the filename and extension from a path.
    MsgBox fs.GetFileName("c:/extension.txt") ' returns extension.txt
    
    'To get the drive name from a path.
    MsgBox fs.GetDriveName("c:/extension.txt") ' returns c:
    End Sub

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