如何检查给定路径是vbscript中的目录或文件?

时间:2014-01-10 02:49:24

标签: regex vbscript

我想检查用户给出的路径是vbscript中的目录还是文件。有没有正则表达式或其他方法可以做到这一点?任何帮助都会很棒。

2 个答案:

答案 0 :(得分:4)

Function GetFSElementType( ByVal path )
    With CreateObject("Scripting.FileSystemObject") 
        path = .GetAbsolutePathName( path )
        Select Case True
            Case .FileExists(path)   : GetFSElementType = 1
            Case .FolderExists(path) : GetFSElementType = 2
            Case Else                : GetFSElementType = 0
        End Select
    End With
End Function

Function IsFile( path )
    IsFile = ( GetFSElementType(path) = 1 )
End Function

Function IsFolder( path )
    IsFolder = (GetFSElementType(path) = 2 )
End Function

Function FSExists( path )
    FSExists = (GetFSElementType(path) <> 0)
End Function

WScript.Echo CStr( IsFile("c:\") )
WScript.Echo CStr( IsFolder("c:\") )
WScript.Echo CStr( FSExists("c:\") )

答案 1 :(得分:2)

将此函数添加到您的代码中并使用它,随时将sAns更改为某个公共Const。

Function IsFileOrFolder(sInputText)
    Dim sAns, oFSO
    sAns = "No such a File or Folder!"
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FileExists(sInputText) Then sAns = "FILE: " & sInputText
    If oFSO.FolderExists(sInputText) Then sAns = "FOLDER: " & sInputText
    Set oFSO = Nothing
    IsFileOrFolder = sAns
End Function