C:\_G\WWW\~ELISANET\INFO\tscmd177.html
<http://www.elisanet.fi/tsalmi/info/tscmd177.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Tue 11-Dec-2018 19:03:28

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



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


References/Comments: (If a Google message link fails try the links within the brackets.)
  FullName Property (WshShortcut Object)
  Google Groups 11 Sep 2009 20:41:44 +0100 [M]
  Google Groups 15 Jun 2010 13:36:08 +0100 [M]
  Google Groups16 Jun 2010 00:21:18 +0300 [M]

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd177.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd177
http://www.elisanet.fi/tsalmi/info/tscmd177.html
file:///c:/_g/www/~elisanet/info/tscmd177.html