如何在Visual Basic Script中确定路径是相对路径还是绝对路径。
在VBA中,应调用Win32 Api函数PathIsRelative。
Private Declare Function PathIsRelative Lib "shlwapi" _
Alias "PathIsRelativeA" _
(ByVal pszPath As String) As Long
然而,从VBS中无法调用DLL,因此我无法使用Win32 API。
René
如何在Visual Basic Script中确定路径是相对路径还是绝对路径。
在VBA中,应调用Win32 Api函数PathIsRelative。
Private Declare Function PathIsRelative Lib "shlwapi" _
Alias "PathIsRelativeA" _
(ByVal pszPath As String) As Long
然而,从VBS中无法调用DLL,因此我无法使用Win32 API。
René
set oFSO = CREATEOBJECT("Scripting.FileSystemObject")
relativePath = ""
absolutePath = "c:\test"
MsgBox UCase(relativePath) = UCase(oFSO.GetAbsolutePathName(relativePath))
MsgBox UCase(absolutePath) = UCase(oFSO.GetAbsolutePathName(absolutePath))
否则,根据该页面,该路径是相对路径。
- 任何格式的 UNC 名称,始终以两个反斜杠字符("
\\
")开头。- 带有反斜杠的磁盘标识符,例如 "
C:\
" 或 "d:\
"。- 单个反斜杠,例如 "
\directory
" 或 "\file.txt
"。
CON
)似乎超出了范围。Function isAbsolutePath(path)
isAbsolutePath = True
Dim first : first = UCase(Left(path, 1))
Dim secondNthird : secondNthird = UCase(Mid(path, 2, 2))
If first > "A" and first < "Z" and secondNthird = ":\" Then Exit Function
If first = "\" Then Exit Function
isAbsolutePath = False
End Function
测试代码:
Function IIf(clause, thenValue, elseValue)
If CBool(clause) Then
IIf = thenValue
Else
IIf = elseValue
End If
End Function
For Each path in Array ( _
"C:\Test1", _
"D:\Test2\", _
"3:\Test4", _
"CD:\Test5", _
"\\Test6\", _
"\Test7", _
"Test8", _
".\Test9\", _
"..\Test10" _
)
Response.Write path & ": " & IIf(isAbsolutePath(path), "absolute", "relative") & "</br>"
Next
输出:
C:\Test1: absolute
D:\Test2\: absolute
3:\Test4: relative
CD:\Test5: relative
\\Test6\: absolute
\Test7: absolute
Test8: relative
.\Test9\: relative
..\Test10: relative
3:\Test4
是相对但非法的)。dim position
position = InStr("your-path", ":")
If position > 0 Then
''# absolute path
else
''# relative path
end if
C:\file.txt
是完整路径,而 C:file.txt
则是相对路径(请参见 http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#fully_qualified_vs._relative_paths)。 - HelenC:file.txt
指的是驱动器 C
上当前文件夹中的 file.txt
文件。同样地,C:folder\file.txt
指的是当前文件夹下 folder
子文件夹中的文件;C:..\file.txt
则指向当前文件夹的父文件夹中的文件,以此类推。 - Helen\\some\share\file.txt
is another absolute path which does not contain a :
- bacarFUNCTION IsPathAbsolute( testedPath)
set oFSO = CREATEOBJECT("Scripting.FileSystemObject")
IsPathAbsolute = UCASE( testedPath) = UCASE( oFSO.GetAbsolutePathName( testedPath))
C:\foo\..\bar
将被规范化为C:\bar
。 - peterchen