4

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?

4
  • 1
    I think you are going about it kinda in a wrong way. Please refer to How Can I Open CMD in specific Folder By that cmd /K "cd /d C:\Folder\Folder will open CMD in C:\Folder\Folder
    – Darius
    Sep 3, 2014 at 23:20
  • Thanks. But I dont want to go that way. 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. Sep 4, 2014 at 20:54
  • @davexpression: Can you explain why Darius' suggestion is not practical? That's the proper way of doing things, so that when you click on the CMD shortcut it will automatically open at your specified directory.
    – Karan
    Sep 6, 2014 at 3:35
  • 2
    Darius' suggestion is practical. Sometimes, it's not about speed or alternatives but possibilities. But thanks @Darius, the link helped Sep 7, 2014 at 20:39

4 Answers 4

4

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"
0
5

In case anyone got to this question like I did, wanting to decipher a .lnk file using only the command prompt, try:

type mylink.lnk|find "\"
1
  • 2
    For me is the right answer. Thanks. Aug 5, 2021 at 8:55
1

I think the problem is a .lnk file is a document that is interpreted by a program - Explorer.exe - just like a docx file would be handled by Word, or whatever. You'd have to write some sort of script to parse the lnk file and excecute a cd command.

It is possible there is the ability to do what you want in PowerShell (as opposed to Command Prompt) through a cmdlet, either built-in, third-party, or one you could write yourself, but I do not know.

0
0

Assume you passed a full path as parameter in batch

REM Check if %1 is shortcut(.lnk) or not
if /i "%~x1" neq ".lnk" (goto :itsNotShortcut)

REM if the passed parameter is a shortcut
REM set full path of shortcut
set "shortcut_path=%~1"

REM Extract target from shortcut
for /f "delims=" %%a in ('wmic path win32_shortcutfile where "name='%shortcut_path:\=\\%'" get target /value') do (
for /f "tokens=2 delims==" %%b in ("%%~a") do (
  set target_path=%%~b
  set target_name=%%~nxb
  )
)
echo Target path: %target_path%
echo Target name: %target_name%

REM if the passed parameter is not shortcut
:itsNotShortcut

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .