Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to read target of .lnk file with command line  (Read 77219 times)

0 Members and 2 Guests are viewing this topic.

gh0std0g74



    Apprentice

    Thanked: 37
    Re: How to read target of .lnk file with command line
    « Reply #15 on: April 06, 2009, 06:22:41 AM »
    strings.exe can be used to remove unwanted characters.
    Code: [Select]
    C:\Documents and Settings\Administrator\Desktop>strings myshortcut.lnk

    Strings v2.41
    Copyright (C) 1999-2009 Mark Russinovich
    Sysinternals - www.sysinternals.com

    +00
    #C:\
    &:<s1
    Program Files
    PROGRA~1
    &:<s
    Novativa Streamster
    NOVATI~1
    &:=s
    Streamster.exe
    STREAM~1.EXE
    C:\Program Files\Novativa Streamster\Streamster.exe
    O*{
    O*{

    nubia

      Topic Starter


      Rookie

      Re: How to read target of .lnk file with command line
      « Reply #16 on: April 06, 2009, 06:51:14 AM »
      OK So I've got:
      Code: [Select]
      @echo off
      REM example
      echo Set Shortcut=%1
      echo set WshShell = WScript.CreateObject("WScript.Shell")>DecodeShortCut.vbs
      echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>DecodeShortCut.vbs
      echo wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
      echo set vbscript=cscript //nologo DecodeShortCut.vbs
      For /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
      del DecodeShortCut.vbs
      Echo Shortcut %shortcut%
      Echo Target   "%target%">>target.txt
      And the output:
      Code: [Select]
      C:\VB>ShortcutTarget.bat "C:/Documents and Settings/All Users/Desktop/Adobe Acro
      bat 6.0 Professional.lnk"
      Set Shortcut="C:/Documents and Settings/All Users/Desktop/Adobe Acrobat 6.0 Prof
      essional.lnk"
      set vbscript=cscript //nologo DecodeShortCut.vbs
      C:\VB\DecodeShortCut.vbs(2, 1) WshShell.CreateShortcut: The shortcut pathname mu
      st end with .lnk or .url.

      Shortcut
      It seems like it would work if I could get rid of the quotes around the path as it seems to want the last characters to be .lnk
      So can I substring "C:/Documents and Settings/All Users/Desktop/Adobe Acrobat 6.0 Prof
      essional.lnk"
      to read C:/Documents and Settings/All Users/Desktop/Adobe Acrobat 6.0 Prof
      essional.lnk
      in my batch file? I need the quotes in the dos box since the path contains spaces?

      Dias de verano

      • Guest
      Re: How to read target of .lnk file with command line
      « Reply #17 on: April 06, 2009, 09:25:37 AM »
      OK So I've got:
      Code: [Select]
      @echo off
      REM example
      echo Set Shortcut=%1
      ...

      The line with REM example can be removed; it was just to show where an example was in the original batch.

      There should not be an echo at the start of the next line! It should just be set Shortcut=[something]

      Quote
      I need the quotes in the dos box since the path contains spaces?

      Yes, that is correct, and the solution is to pass the shortcut name with quotes from the command line like this

      Code: [Select]
      ShortcutTarget.bat "C:/Documents and Settings/All Users/Desktop/Adobe Acrobat 6.0 Professional.lnk"
      But in the batch strip the quotes off using the tilde ("~") variable modifier: so use %~1 instead of %1 as you see below.



      @echo off
      Set Shortcut=%~1
      echo set WshShell = WScript.CreateObject("WScript.Shell")>DecodeShortCut.vbs
      echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>DecodeShortCut.vbs
      echo wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
      set vbscript=cscript //nologo DecodeShortCut.vbs
      For /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
      del DecodeShortCut.vbs
      Echo Shortcut %shortcut%
      Echo Target   %target%


      « Last Edit: April 08, 2009, 12:24:53 AM by Dias de verano »

      nubia

        Topic Starter


        Rookie

        Re: How to read target of .lnk file with command line
        « Reply #18 on: April 07, 2009, 07:55:58 PM »
        :D I want to thank all of you who have helped me in this.
        Dias especially. You have not only solved the problem you have taught me a lot, for which I am grateful.
        This is a great site and I hope some day to be able to help others.
        But it seems that I am way behind every one and would like to learn more about Dos programming and vb script too.
        I, from necessity, have jumped into the middle of this and think it would be a good idea to  read a primer on the subjects. (Dos programming and vb script).
        My best to all!