如何确定路径是vb6中的文件或目录?

时间:2015-02-23 18:10:12

标签: vb6 filesystems

我正在开发一个项目,我需要从用户那里获取路径并在该路径上做一些事情,但我需要知道该路径是什么。我无法通过检查扩展程序来执行此操作。因为文件可能没有扩展名。是否有任何功能,如is_dir()& php中的is_file()函数?

3 个答案:

答案 0 :(得分:5)

你有没有考虑过明显的问题?

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

答案 1 :(得分: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

答案 2 :(得分:0)

还有一个要使用的功能:Dir$()

使用默认的vbNormal属性参数Dir$()如果pathname参数是目录,则返回空字符串

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