VB6从路径获取文件名

时间:2015-02-20 15:19:23

标签: csv path vb6 filenames

在我的VB6代码中,我有声明

Open CommonDialog.FileName For Input As #intFileNum

问题是CommonDialog.FileName返回一个完整的路径,而VB6 Open命令只需要一个文件名。例如,如果路径是C:\ temp \ file_name.csv,则CommonDialog.FileName将返回C:\ temp \ file_name.csv,但VB6中的此Open语句仅适用于file_name.csv

我可以把

Dim Path As String
Path = CommonDialog.FileName
Open Path For Input As #intFileNem

我只需要知道如何在最后一个反斜杠之前删除Path中的所有内容" \"。 VB6库中有什么东西可以做到吗?

路径是从常用的对话框和代码

中提取的
CommonDialog.DialogTitle = "Browse. . ."
    CommonDialog.Filter = "CSV File (*.csv)|*.csv"
    CommonDialog.ShowOpen

所以我想如果有人知道,我应该要求使用ShowOpen来获取公共子路径,然后在稍后的函数中使用带有Open命令的路径中的文件名不是任何类型的问题它自己的,对吧?

为了便于阅读,我必须在这里展示Ghost。我使用intFileNum作为长

Dim intFileNum As Long

Open Path1 For Input As #intFileNum

Do Until EOF(intFileNum)

Line Input #intFileNum, LineEnd
希望这些信息可以使情况更有意义

3 个答案:

答案 0 :(得分:6)

Function GetFileNameFromPath(strFullPath As String) As String
    GetFileNameFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "\"))
End Function

但是,您的问题是由于您未使用FreeFile或未关闭该文件而导致其被锁定。

Public Function SomeMethod()
    On Error GoTo errSomeMethod

    Dim lngFileHandle As Long

    lngFileHandle = FreeFile
    Open CommonDialog.FileName For Input As #lngFileHandle

    'Whatever you want here

    Close #lngFileHandle

    Exit Function
errSomeMethod:
    Close #lngFileHandle
End Function

答案 1 :(得分:1)

回答实际的原始问题:

在手册中,请参阅FileTitle Property

换句话说,这项工作已经为你完成了......假设你真的需要它。但这是另一个问题。

答案 2 :(得分:-1)

没有内置任何内容,但解析字符串是微不足道的。

这是一个方便的功能。

Function ExtractFile(ByVal PathName As String) As String
    Dim f As String
    Dim n As Integer
' Return the filename portion of a full pathname

    f$ = PathName

    Do
        n% = InStr(f$, "\")
        If n% > 0 Then f$ = Right$(f$, Len(f$) - n%)
    Loop While n% > 0

    Do
        n% = InStr(f$, "/")
        If n% > 0 Then f$ = Right$(f$, Len(f$) - n%)
    Loop While n% > 0

    ExtractFile = f$

End Function