Winapi 如何使用VB脚本主机确定路径是相对路径还是绝对路径

Winapi 如何使用VB脚本主机确定路径是相对路径还是绝对路径,winapi,vbscript,Winapi,Vbscript,如何在Visual Basic脚本中确定路径是相对路径还是绝对路径 在VBA中,我将调用Win32 Api函数PathIsRelative Private Declare Function PathIsRelative Lib "shlwapi" _ Alias "PathIsRelativeA" _ (ByVal pszPath As String) As Long 但是,无法从VBS调用DLL,因此无法使用 Win32 Api 勒内也许这样看起来会更好: FUNCTION I

如何在Visual Basic脚本中确定路径是相对路径还是绝对路径

在VBA中,我将调用Win32 Api函数PathIsRelative

Private Declare Function PathIsRelative Lib "shlwapi" _
    Alias "PathIsRelativeA" _
   (ByVal pszPath As String) As Long
但是,无法从VBS调用DLL,因此无法使用 Win32 Api


勒内

也许这样看起来会更好:

FUNCTION IsPathAbsolute( testedPath)

set oFSO = CREATEOBJECT("Scripting.FileSystemObject")

IsPathAbsolute = UCASE( testedPath) = UCASE( oFSO.GetAbsolutePathName( testedPath))

很晚,但根据Helen评论中引用的Microsoft page,路径是绝对的,如果:

  • 任何格式的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

当然,如前所述,您必须确保下一步您的路径是有效的(
3:\Test4
是相对的,但是非法的);和//都与java/c/c++/等类似。检查冒号的存在是不够的。例如,
C:\file.txt
是一个完整路径,而
C:file.txt
是相对路径(请参阅)。@Helen:非常好的信息,谢谢,但无法确定此路径与何处相关?
C:file.txt
指驱动器
C
上当前文件夹中的
file.txt
文件。类似地,
C:folder\file.txt
指当前文件夹的
文件夹
子文件夹中的文件
C:..\file.txt
--指向当前文件夹的父文件夹中的文件,依此类推。
\\some\share\file.txt
是另一个绝对路径,它不包含
我不明白这是如何回答这个问题的?无法可靠地工作,因为GetAbsolutePathName也规范化了路径,例如,
C:\foo\..\bar
将标准化为
C:\bar
FUNCTION IsPathAbsolute( testedPath)

set oFSO = CREATEOBJECT("Scripting.FileSystemObject")

IsPathAbsolute = UCASE( testedPath) = UCASE( oFSO.GetAbsolutePathName( testedPath))
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