0

I would like to create a shortcut to some directory which I can use in Windows console with a cd command. So I create a directory symbolic link:

mklink /D link_name c:\some\path

/D is for a directory symbolic link (default is a file symbolic link)

Let's say I have created the link link_name in my user's home directory: c:\Users\user\link_name.

I can then use that link with the cd command:

c:\Users\user> cd link_name
c:\Users\user\link_name>

The only problem is the resulting path. I simply need to have the resulting path the same as symbolic link's target, i.e. c:\some\path from my example, but it's c:\Users\user\link_name instead.

So the directory symbolic link is rather a directory alias than a shortcut.

What I need:

c:\Users\user> cd link_name
c:\some\path> cd ..
c:\some>

How the directory symbolic link actually works:

c:\Users\user> cd link_name
c:\Users\user\link_name> cd ..
c:\Users\user>

Is there any way to achieve what I need? I prefer some file system object over defining an environment variable, etc.

1 Answer 1

2

This is not how a symbolic link works.

What you want to do is not directly possible, but there are methods you can use that are similar.

For example, you can place a .cmd file in that folder that changes the folder to the new one, and you then type the name of the .cmd file to go to the other folder. If you store this .cmd file in a folder that is also listed in the %PATH% environmental variable, you can type the name of this cmd anywhere to redirect to that new folder.

The .cmd would simply have the cd command in it. Example:

::cd_link_name.cmd
cd /d c:\some\path

The downside is that no program will follow that link.

If you want a program to follow the link you really do need mklink to create a junction and yes, that will mean that to the system the virtual folder seems to be at the location of the junction but it really does redirect. There's simply no way around that.

That said, if your aim is just to be able to quickly navigate favorite folders from the command line, consider writing a global batch file that you store at a place that is mentioned in the %PATH% and let that change the folder for you.

An example of such cmd file would be:

:: cdf.cmd
@echo off

:: lets see if the first parameter is recognized. If so, change folder.
:: note, you can't use spaces in these identifiers, but you can keep them
::  short and to the point.

:: c forwards to c:\
if "%1"=="c" cd /d c:\

:: desk forwards to your Desktop folder
if "%1"=="desk" cd /d %USERPROFILE%\desktop

::docs forwards to your My Documents folder 
if "%1"=="docs" cd /d %USERPROFILE%\Documents

::a1 forwards to e:\some\path\special
if "%1"=="a1" cd /d e:\some\path\special

and so on....

You can do this:

C:\Windows\System32>cdf a1

E:\Some\Path\Special>cdf desk

C:\Users\Username\Desktop>_

btw, I named it cdf for: Change Directory Favorites. :) But any name will do of course.

1
  • 1
    In case a double quoted argument is passed or different casing you should always use if /i "%~1"==...
    – LotPings
    Commented Jul 26, 2018 at 21:49

You must log in to answer this question.

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