从快捷方式链接中提取路径 - Windows批处理

4
在Windows批处理文件中,如何提取快捷方式链接的路径而不使用VBScript?
2个回答

8

您可以使用 wmic 查询 win32_shortcutfile 来实现。确保在 %filename% 中所有反斜杠都被转义。

语法:

batfile shortcutfile.lnk

代码:

@echo off
setlocal

rem // ensure user supplied a filename with a .lnk extension
if /i "%~x1" neq ".lnk" (
    echo usage: %~nx0 shortcut.lnk
    goto :EOF
)

rem // set filename to the fully qualified path + filename
set "filename=%~f1"

rem // get target
for /f "delims=" %%I in (
    'wmic path win32_shortcutfile where "name='%filename:\=\\%'" get target /value'
) do for /f "delims=" %%# in ("%%~I") do set "%%~#"

rem // preserve ampersands
setlocal enabledelayedexpansion
echo(!target!

1
你可以尝试使用shortcutjs.bat
call shortcutjs.bat "some.lnk"^| find /i "target:"

与VBScript一样,这也使用Windows脚本宿主,但使用的是另一种内置语言 - JScript,但包装在一个.bat文件中。使用纯批处理程序无法提取目标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接