Extract Path from shortcut link - windows batch

后端 未结 2 1060
悲&欢浪女
悲&欢浪女 2021-01-20 17:52

How can I extract the path from a shortcut link in windows batch file without using vbscript?

相关标签:
2条回答
  • 2021-01-20 18:41

    You can do it with a wmic query to win32_shortcutfile. Just make sure all your backslashes are backslash-escaped within %filename%.

    Syntax:

    batfile shortcutfile.lnk
    

    Code:

    @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!
    
    0 讨论(0)
  • 2021-01-20 18:42

    You can try with shortcutjs.bat

    call shortcutjs.bat "some.lnk"^| find /i "target:"
    

    Like vbscript this also uses windows script host , but with the other built-in language - jscript , but wrapped in a .bat file. Extracting the target will be not possible with pure batch.

    0 讨论(0)
提交回复
热议问题