我正在处理一个项目,需要用户提供路径并对该路径进行一些操作,但我需要知道那个路径是什么。我不能通过检查扩展名来做到这一点。因为有些文件可能没有扩展名。在php中是否有像is_dir()
和is_file()
函数这样的函数?
我正在处理一个项目,需要用户提供路径并对该路径进行一些操作,但我需要知道那个路径是什么。我不能通过检查扩展名来做到这一点。因为有些文件可能没有扩展名。在php中是否有像is_dir()
和is_file()
函数这样的函数?
If GetAttr(Path) And vbDirectory Then
MsgBox "Directory"
Else
MsgBox "Not directory"
End If
(GetAttr(Path) And vbDirectory) <> 0
更加明确。 - wqw还有一个可用的函数: 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
检查原始字符串是否也是一个有效的字符串。
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