Reputation: 313
I want to check the path given by user is a directory or a file in vbscript. Is there any regular expression or the other ways I can do this ? Any help would be great.
Upvotes: 3
Views: 4445
Reputation: 70923
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:\") )
Upvotes: 5
Reputation: 6433
Add this function to your code and use it, feel free to change sAns to some public 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
Upvotes: 3