177} How can I retrieve the target of a shortcut into a variable?
@echo off & setlocal enableextensions
if "%~1"=="" (
echo Usage: "%~n0" ["The link's full path"]
goto :EOF)
::
if /i "%~x1" NEQ ".lnk" (
echo Not a shortcut
goto :EOF)
::
:: Build a Visual Basic Script
set vbs_=%temp%\tmp$$$.vbs
set skip=
findstr "'%skip%VBS1" "%~f0" > "%vbs_%"
::
:: Run it with Microsoft Windows Script Host Version 5.6
:: Allow for hyphens (') in the target file names
for /f "
usebackq tokens=* delims=" %%a in (
`cscript //nologo "%vbs_%" "%~1"
`) do set target_=%%a
::
:: Display the result
echo "%~1"
echo links to
echo "%target_%"
::
:: Clean up
for %%f in ("%vbs_%") do del %%f
endlocal & goto :EOF
'
'............................................
'The Visual Basic Script
'
Set WshShell = WScript.CreateObject("WScript.Shell") 'VBS1
Set arg = WScript.Arguments 'VBS1
Set oShellLink = WshShell.CreateShortcut(arg(0)) 'VBS1
WScript.Echo oShellLink.TargetPath 'VBS1
The output could be e.g.
C:\_D\TEST>cmdfaq "C:\_L\TIMO\EXTENS03\EXCELVB.TXT.lnk"
"C:\_L\TIMO\EXTENS03\EXCELVB.TXT.lnk"
links to
"C:\_D\EXCEL\EXCELVB.TXT"
If the above fails for the shortcuts on the
Desktop try this alternative:
@echo off & setlocal enableextensions
::
if "%~1"=="" (
echo Usage: "%~n0" ["The link's name on the Desktop, no path"]
goto :EOF)
::
:: Build a Visual Basic Script
set vbs_=%temp%\tmp$$$.vbs
set skip=
findstr "'%skip%VBS2" "%~f0" > "%vbs_%"
::
:: Run it with Microsoft Windows Script Host Version 5.6
for /f "usebackq tokens=* delims=" %%a in (
`cscript //nologo "%vbs_%" "%~1"`) do set target_=%%a
::
:: Display the result
echo.%~1
echo on the Desktop links to
echo.%target_%
::
:: Clean up
for %%f in ("%vbs_%") do del %%f
endlocal & goto :EOF
'
'............................................
'The Visual Basic Script
'
Set WshShell = WScript.CreateObject("WScript.Shell") 'VBS2
Set arg = WScript.Arguments 'VBS2
strDesktop = WshShell.SpecialFolders("Desktop") 'VBS2
Set oShellLink = WshShell.CreateShortcut(
strDesktop + "
\\" + arg(0)) 'VBS2
WScript.Echo oShellLink.TargetPath 'VBS2
The output could be e.g.
C:\_D\TEST>cmdfaq "CLIPDATE.lnk"
CLIPDATE.lnk
on the Desktop links to
C:\_F\XTOOLS\CLIPDATE.CMD
If the response is empty, the link probably is not truly on your
Desktop but e.g. on All Users'.
True-life usage:
@echo off & setlocal enableextensions
echo +------------------------------------------------------------------+
echo ^| TSE32SCUT.CMD Run The SemWare Editor on the target of a shortcut ^|
echo ^| By Prof. Timo Salmi, Last modified Fri 11-Sep-2009 ^|
echo +------------------------------------------------------------------+
echo.
::
:: If it is not a shortcut
if /i %~x1 NEQ .lnk (
if exist "%~f1" if not exist "%~f1\" (
start "" "C:\Program Files\TSEPro\tse32" "%~f1")
goto :EOF)
::
:: Build a Visual Basic Script
set vbs_=%temp%\tmp$$$.vbs
set skip=
findstr "'%skip%VBS1" "%~f0" > "%vbs_%"
::
:: Run it with Microsoft Windows Script Host Version 5.6
for /f "
usebackq tokens=* delims=" %%a in (
`cscript //nologo "%vbs_%" "%~1"
`) do set target_=%%a
::
:: Clean up
for %%f in ("%vbs_%") do del %%f
::
:: Run The SemWare Editor on the target file
if "%target_%"=="" (
echo Target not resolved
pause
goto :EOF)
if not exist "%target_%" (
echo Target "%target_%" not found
pause
goto :EOF)
if exist "%target_%\" (
echo The target "%target_%" is a folder
pause
goto :EOF)
if exist "%target_%" start "" "C:\Program Files\TSEPro\tse32" "%target_%"
endlocal & goto :EOF
'
'............................................
'The Visual Basic Script
'
Set WshShell = WScript.CreateObject("WScript.Shell") 'VBS1
Set arg = WScript.Arguments 'VBS1
Set oShellLink = WshShell.CreateShortcut(arg(0)) 'VBS1
WScript.Echo oShellLink.TargetPath 'VBS1
Also a pure script solution exists, but, as is so common, it does not
tolerate poison characters.
@echo off & setlocal enableextensions
set "target_="
if "%~1" NEQ "" (
if exist "%~1" (
if /i "%~x1" equ ".lnk" (
for /f "tokens=*" %%# in ('
more "%~f1"^|findstr ":\\."') do (
set "target_=%%~dpf#"))))
if not defined target_ goto :EOF
::
:: Display the result
echo.%target_%
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq "C:\_L\TIMO\EXTENS03\EXCELVB.TXT.lnk"
C:\_D\EXCEL\EXCELVB.TXT