2015-02-23 215 views
2

我正在開發一個項目,我需要從用戶那裏獲得一條路徑,並在那條路上做某些事情,但我需要知道那條路是什麼。我無法通過檢查擴展名來做到這一點。因爲一個文件可能沒有擴展名。在函數中有沒有像is_dir() & is_file()函數的函數?如何確定路徑是vb6中的文件或目錄?

+0

io.file.exists Dir$()返回空字符串()和io.directory.exists() – 2015-02-23 18:12:51

+0

是否需要添加引用? – 2015-02-23 18:16:35

+2

VB6或VB.NET?它*確實*重要的課程VB6的 – Plutonix 2015-02-23 18:18:56

回答

1

檢查原始字符串是否也是有效的字符串。

Function FileOrFolder(strg As String) 
    Dim fs, FExists, DirExists 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    FExists = fs.FileExists(strg) 
    DirExists = fs.folderexists(strg) 
    If FExists = True Then 
    FileOrFolder = "It's a file"     '// file 
    ElseIf DirExists = True Then 
    FileOrFolder = "It's a folder"    '// folder 
    Else 
    FileOrFolder = "Neither a file nor a folder" '// user string invalid 
    End If 
End Function 
4

你認爲明顯嗎?

If GetAttr(Path) And vbDirectory Then 
    MsgBox "Directory" 
Else 
    MsgBox "Not directory" 
End If 
+0

加一個。值得一提的是,如果路徑不存在,它會引發錯誤。 – MarkJ 2015-02-24 12:29:34

+0

'(GetAttr(Path)and vbDirectory)<> 0'更明確 – wqw 2015-02-25 08:17:05

0

多一個函數使用:Dir$()

在缺省vbNormal屬性參數,如果路徑名參數是一個目錄

Private Sub Command1_Click() 
    Dim strPath As String 
    Dim strFile As String 
    strPath = "c:\temp" 
    strFile = "c:\temp\pic.bmp" 
    Print strPath & " : " & CStr(IsDir(strPath)) 
    Print strFile & " : " & CStr(IsDir(strFile)) 
End Sub 

Private Function IsDir(strPath As String) As Boolean 
    If Len(Dir$(strPath, vbNormal)) = 0 Then 
    IsDir = True 
    Else 
    IsDir = False 
    End If 
End Function 
相關問題