Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Path from shortcut link - windows batch

Tags:

batch-file

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

like image 991
Starfish Avatar asked Sep 10 '25 17:09

Starfish


2 Answers

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!
like image 81
rojo Avatar answered Sep 13 '25 10:09

rojo


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.

like image 24
npocmaka Avatar answered Sep 13 '25 09:09

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!