Reputation: 13
I have to extract source filename from .lnk shortcut in batch. The extracted text must be in (program name).(extension) form.
I must admit that I'm a complete laic, when it comes to batch (or any scripting actually), so any help concerning my question is highly appreciated.
Upvotes: 1
Views: 2139
Reputation: 24476
You can do it with a wmic
query to win32_shortcutfile. Just make sure all your backslashes are backslash-escaped within %filename%
.
@echo off
setlocal
:: ensure user supplied a filename with a .lnk extension
if /i "%~x1" neq ".lnk" (
echo usage: %~nx0 shortcut.lnk
goto :EOF
)
:: set filename to the fully qualified path + filename
set "filename=%~f1"
:: convert single backslashes to double
set "filename=%filename:\=\\%"
:: get target
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
echo(%%J
)
What you want ends up in %%J
. If you only want the target filename.ext
, change that to %%~nxJ
. If you want only the drive and path, change it to %%~dpJ
. See the last page of help for
in a cmd console for more info about variable expansion.
Upvotes: 5