【问题标题】:TargetPath is blank - on remote drivesTargetPath 为空白 - 在远程驱动器上
【发布时间】:2015-09-06 08:59:44
【问题描述】:

我似乎无法让 WShell 返回 objShortcut.TargetPath 的值,尽管它可以很好地传递全名。

我一直在阅读 WShell 可能存在远程磁盘问题,并且我一直在使用外部驱动器。

在我的 C: 驱动器上使用位于我的 C: 驱动器上的文件的快捷方式对其进行测试后,我发现它仍然无法正常工作。它不是回显 traget 路径,而是回显一个空白值。

已编辑。感谢您的提示。

getshorty.vbs

Dim objWSHShell
set objWSHShell = CreateObject("WScript.Shell")  
Set wshShell    = WScript.CreateObject("WScript.Shell")  
strTargetPath=objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))  
Set objShortcut = wshShell.CreateShortcut(strTargetPath)  
WScript.Echo objShortcut.TargetPath  
Set objShortcut = Nothing  
Set wshShell    = Nothing  

【问题讨论】:

  • edit你的问题(不要发表评论)并添加来自WScript.Echo WScript.Arguments.Item(0)WScript.Echo strTargetPath的示例输出。

标签: vbscript shortcut


【解决方案1】:

这是一个创建快捷方式的函数:

Call Shortcut("C:\The Absolute Path of your application goes here","Name of your Shortcut")
'*********************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-25"
    objShortCut.Save
End Sub
'*********************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************

【讨论】:

    【解决方案2】:

    当然,如果.lnk.url 文件不存在,则TargetPath 属性结果为""(长度为零的字符串)。

    CreateShortcut 方法返回 WshShortcut 对象或 WshURLShortcut 对象。只需调用CreateShortcut 方法即可打开现有快捷方式,但不会创建快捷方式。

    option explicit
    On Error GoTo 0
    
    Dim wshShell, strTargetPath, objShortcut 
    Set wshShell  = WScript.CreateObject("WScript.Shell")
    strTargetPath = wshShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    
    Set objShortcut = wshShell.CreateShortcut(strTargetPath)
    
    WScript.Echo TypeName(objShortcut) & vbTab & VarType(objShortcut) _
               & vbNewLine & "FullName"    & vbTab & objShortcut.FullName _
               & vbNewLine & "TargetPath"  & vbTab & objShortcut.TargetPath
    If TypeName(objShortcut) = "IWshShortcut" Then            
      WScript.Echo      "Arguments"   & vbTab & objShortcut.Arguments _
          & vbNewLine & "Description" & vbTab & objShortcut.Description _
          & vbNewLine & "WorkingDir"  & vbTab & objShortcut.WorkingDirectory
    End If
    

    输出

    ==>dir /B d:\xxxx\*Shortcut.*
    32421790 Shortcut.url
    pisma - Shortcut.lnk
    
    ==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\32421790 Shortcut.url"
    IWshURLShortcut 8
    FullName        d:\xxxx\32421790 Shortcut.url
    TargetPath      http://stackoverflow.com/q/32421790/3439404
    
    ==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\nonexistent Shortcut.url"
    IWshURLShortcut 8
    FullName        d:\xxxx\nonexistent Shortcut.url
    TargetPath
    
    ==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\pisma - Shortcut.lnk"
    IWshShortcut    8
    FullName        d:\xxxx\pisma - Shortcut.lnk
    TargetPath      D:\bat\SU\Files\ruzna pisma.png
    Arguments
    Description     font samples
    WorkingDir      D:\bat\SU\Files
    
    ==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\nonexistent Shortcut.lnk"
    IWshShortcut    8
    FullName        d:\xxxx\nonexistent Shortcut.lnk
    TargetPath
    Arguments
    Description
    WorkingDir
    
    ==>
    

    【讨论】:

    • 我遇到了与第二个示例中的输出相同的问题。目标路径为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多