locked
How to get name of file RRS feed

  • Question

  • In a folder I have a file called "file1.grf"

    There may also be a file called "file1_zx.grf"

    I dont know the exact name - only that it will be like "file1_*.grf"

    I have the path of the folder, and I know the name "file1.frg".

    How do I get the name of the file "file1_zx.grf" if it exists?

    I would like to have a function like

    Public Function fileName(name as string, path As string) As string

    that returns the proper name or blank if it does not exist.

    I have rutines like - and I hope to make something similar:

    Public Function file_exists(path As Variant) As Boolean
        Dim FS As Object
        file_exists = False
        Set FS = CreateObject("scripting.filesystemobject")
        If Not IsNull(path) Then
            If FS.FileExists(path) Then
                file_exists = True
            End If
        End If
    End Function

    Public Function folder_exists(path As Variant) As Boolean
        Dim FS As Object
        folder_exists = False
        Set FS = CreateObject("scripting.filesystemobject")
        If Not IsNull(path) Then
            If FS.folderexists(path) Then
                folder_exists = True
            End If
        End If
    End Function

    Wednesday, February 4, 2015 5:18 PM

Answers

  • Hi,

    try sth like:

    Public Function fctGiveFileName(strPath As String, strPattern As String) As String
    ' example call: fctGiveFileName "c:\fubar\", "file1_*.grf"

    Dim strFile As String

    strFile = Dir(strPath)
     
    Do While strFile <> ""
        strFile = Dir
        If strFile Like strPattern Then
            ' fctGiveFileName = strFile
            MsgBox strFile
        End If
    Loop

    End Function


    cu
    Karl
    ******
    Access FAQ (de/it): http://www.donkarl.com
    Access Lobby: http://www.AccessDevelopers.org

    • Edited by Karl DonaubauerMVP Wednesday, February 4, 2015 5:49 PM sig format
    • Proposed as answer by L.Hl Friday, February 13, 2015 7:36 AM
    • Marked as answer by L.Hl Sunday, February 15, 2015 10:15 AM
    Wednesday, February 4, 2015 5:48 PM
  • Hi,

    << How do I get the name of the file "file1_zx.grf" if it exists?

    Before you get the name the file, first you need to be sure the file existed, then get the file name from the path. Here are ways you can refer to

    ' judge the file exists or not

    Function IsFileExists(ByVal strFileName As String) As Boolean

    If Dir(strFileName, 16) <> Empty Then

        IsFileExists = True

    Else

        IsFileExists = False

    End If

    End Function

    ' judge the file exists or not

    Function IsFileExists(ByVal strFileName As String) As Boolean 

    Dim objFileSystem As Object 

    Set objFileSystem = CreateObject("Scripting.FileSystemObject") 

    If objFileSystem.fileExists(strFileName) = True Then 

    IsFileExists = True 

    Else 

    IsFileExists = False 

    End If 

    End Function

    ' Get the file name

    Function GetFilenameFromPath(ByVal strPath As String) As String

    ' Returns the rightmost characters of a string upto but not including the rightmost '\'

        If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then

            GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)

        End If

    End Function

    Sub Run()

       If IsFileExists("D:\vba\abc.txt") = True Then

       FileName = GetFilenameFromPath("D:\vba\abc.txt")

        MsgBox (FileName)

          

    Else

           MsgBox "No releevant file"

        End If

    End Sub

    Hope this could help you.

    Best Regards

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by L.Hl Friday, February 13, 2015 7:36 AM
    • Marked as answer by L.Hl Sunday, February 15, 2015 10:15 AM
    Thursday, February 5, 2015 10:16 AM

All replies

  • Hi,

    try sth like:

    Public Function fctGiveFileName(strPath As String, strPattern As String) As String
    ' example call: fctGiveFileName "c:\fubar\", "file1_*.grf"

    Dim strFile As String

    strFile = Dir(strPath)
     
    Do While strFile <> ""
        strFile = Dir
        If strFile Like strPattern Then
            ' fctGiveFileName = strFile
            MsgBox strFile
        End If
    Loop

    End Function


    cu
    Karl
    ******
    Access FAQ (de/it): http://www.donkarl.com
    Access Lobby: http://www.AccessDevelopers.org

    • Edited by Karl DonaubauerMVP Wednesday, February 4, 2015 5:49 PM sig format
    • Proposed as answer by L.Hl Friday, February 13, 2015 7:36 AM
    • Marked as answer by L.Hl Sunday, February 15, 2015 10:15 AM
    Wednesday, February 4, 2015 5:48 PM
  • Hi,

    << How do I get the name of the file "file1_zx.grf" if it exists?

    Before you get the name the file, first you need to be sure the file existed, then get the file name from the path. Here are ways you can refer to

    ' judge the file exists or not

    Function IsFileExists(ByVal strFileName As String) As Boolean

    If Dir(strFileName, 16) <> Empty Then

        IsFileExists = True

    Else

        IsFileExists = False

    End If

    End Function

    ' judge the file exists or not

    Function IsFileExists(ByVal strFileName As String) As Boolean 

    Dim objFileSystem As Object 

    Set objFileSystem = CreateObject("Scripting.FileSystemObject") 

    If objFileSystem.fileExists(strFileName) = True Then 

    IsFileExists = True 

    Else 

    IsFileExists = False 

    End If 

    End Function

    ' Get the file name

    Function GetFilenameFromPath(ByVal strPath As String) As String

    ' Returns the rightmost characters of a string upto but not including the rightmost '\'

        If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then

            GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)

        End If

    End Function

    Sub Run()

       If IsFileExists("D:\vba\abc.txt") = True Then

       FileName = GetFilenameFromPath("D:\vba\abc.txt")

        MsgBox (FileName)

          

    Else

           MsgBox "No releevant file"

        End If

    End Sub

    Hope this could help you.

    Best Regards

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by L.Hl Friday, February 13, 2015 7:36 AM
    • Marked as answer by L.Hl Sunday, February 15, 2015 10:15 AM
    Thursday, February 5, 2015 10:16 AM
  • Hi Karl,

    this clearly Works. Thanks.

    Is there a way to limit the result of the Dir command?

    If the folder has an extreme amount of files, the while loop may be take a lot of time...

    I can shorten the loop if I simply exit when I find the first match - but still if it is the last file of many - it may take some time.

    Not in "actual time" - but since this is a completely secondary routine - I am afraid if this loop occupies ressources from the primary functions of the application.

    Thursday, February 5, 2015 3:48 PM
  • Is there a way to limit the result of the Dir command?

    If the folder has an extreme amount of files, the while loop may be take a lot of time...

    I can shorten the loop if I simply exit when I find the first match - but still if it is the last file of many - it may take some time.

    Not in "actual time" - but since this is a completely secondary routine - I am afraid if this loop occupies ressources from the primary functions of the application.

    Hi,

    Dir() is quite fast. For some thousand files you may not notice it. If you have more and do notice I can't think of much else with this approach.

    If you would know more about the file, e.g. all possible name variations you could do a direct Dir() for all of them without looping through the directory. If you would know that there are always 2 unknown characters you could try ?? instead of * etc.

    If you really get a performance problem you should google or ask further for a FileSystemObject solution and test if it's faster. I never use it because I avoid external functions wherever possible and always could do with Dir() so far.


    cu
    Karl

    Thursday, February 5, 2015 4:44 PM