在vb6中如何确定路径是文件还是目录?

3

我正在处理一个项目,需要用户提供路径并对该路径进行一些操作,但我需要知道那个路径是什么。我不能通过检查扩展名来做到这一点。因为有些文件可能没有扩展名。在php中是否有像is_dir()is_file()函数这样的函数?


io.file.exists() 和 io.directory.exists() - Marc B
需要添加引用吗? - hmak.me
2
VB6还是VB.NET?这确实很重要。 - Ňɏssa Pøngjǣrdenlarp
当然可以。我可以用VB.NET处理它。 - hmak.me
3个回答

7
你有没有考虑过显而易见的事情?
If GetAttr(Path) And vbDirectory Then
    MsgBox "Directory"
Else
    MsgBox "Not directory"
End If

加一。值得一提的是,如果路径不存在,它将引发错误。 - MarkJ
1
(GetAttr(Path) And vbDirectory) <> 0 更加明确。 - wqw

1

还有一个可用的函数: Dir$()

使用默认的 vbNormal 属性参数,如果路径名为目录,则 Dir$() 返回一个空字符串。

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

0

检查原始字符串是否也是一个有效的字符串。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接