【发布时间】:2015-01-18 12:43:08
【问题描述】:
我的目标是使用 Vb 脚本查找给定的输入是 s 文件名还是文件夹名。
例如,如果用户给出“D:\Temp\testfile.docx”,那么它应该导航到文件相关功能, 同样,如果用户给出“D:\Temp\”,那么它应该导航到文件夹相关功能。
如果没有直接的解决方案,是否有任何解决方法可以做到这一点?
【问题讨论】:
标签: string file input vbscript directory
我的目标是使用 Vb 脚本查找给定的输入是 s 文件名还是文件夹名。
例如,如果用户给出“D:\Temp\testfile.docx”,那么它应该导航到文件相关功能, 同样,如果用户给出“D:\Temp\”,那么它应该导航到文件夹相关功能。
如果没有直接的解决方案,是否有任何解决方法可以做到这一点?
【问题讨论】:
标签: string file input vbscript directory
检查用户输入是否引用了现有文件 (.FileExists) 或文件夹 (.FolderExists):
If FolderExists(userinp) Then
folderaction
Else
If FileExists(userinp) Then
fileaction
Else
handle bad user input
End If
End If
如果这不符合您的需要,请始终附加“\”让用户识别文件夹并检查Right(userinp, 1)。
【讨论】:
FolderExists 函数。你总是需要一个Scripting.FileSystemObject 实例。
我创建了这个简单的函数来满足你的需要:
'Return 1 if the provided path is a folder, 2 if it's a file, and -1 if it's neither.
Function GetTypeOfPath(strToTest)
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If(objFSO.FolderExists(strToTest)) Then
GetTypeOfPath = 1
ElseIf(objFSO.FileExists(strToTest)) Then
GetTypeOfPath = 2
Else 'neither
GetTypeOfPath = -1
End If
End Function
您可以通过创建一个文件“c:\test”并运行`MsgBox(GetTypeOfPath("c:\test"))"来测试它;它会返回2。然后删除该文件,创建一个文件夹“c: \test" 并运行相同的东西;它会返回 1。然后删除它并第三次运行它;它会返回 -1。
【讨论】:
GetTypeOfPath = -1