Windows – How to parse a .LNK shortcut from the Command Prompt in Windows


I have a shortcut to a folder and I want to open it from the Command Prompt. I want the Command Prompt to immediately change to the target location of the shortcut. Instead, if I try to execute the shortcut at the Command Prompt it simply opens the folder in Windows Explorer.

How can I parse a shortcut file (.LNK) from the Command Prompt and switch to the shortcut's target folder?

Best Answer

  • When I start CMD, I see C:\Users\dave> but I want to change directory to C:\wamp\www without having to press cd ../../. So I created a shortcut of the 'www' folder in the 'dave' folder.

    This is a typical X-Y problem. If you want CMD to always open at a specific directory instead of the default, all you need to do is simply change the shortcut's properties as follows:

    enter image description here

    In Windows 7 the Command Prompt shortcut is typically located in Start Menu > All Programs > Accessories, so just right-click the shortcut, select Properties and edit the Start in field to your liking.

    You can also create a batch file named for example d.bat that contains a single line cd /d c:\wamp\www. Place the batch file somewhere in your path and now all you need to do is open CMD and type d to change to the specific directory. There are many more similar solutions as well.

    If you are dead set on parsing a shortcut (.LNK) file from the command prompt, save the following as ParseLnk.bat and execute it from the Command Prompt as ParseLnk <LNK File>:

    @echo off
    echo set WshShell = WScript.CreateObject("WScript.Shell")>Tmp.vbs
    echo set Lnk = WshShell.Createshortcut(WScript.Arguments(0))>>Tmp.vbs
    echo WScript.Echo Lnk.TargetPath>>Tmp.vbs
    for /f "delims=" %%d in ('cscript //nologo Tmp.vbs "%~1"') do del Tmp.vbs & cd /d "%%d"