Windows CMD: How to create symbolic link to execut

2020-05-23 12:38发布

问题:

My goal is to add a few executables to my PATH (for example, chrome), so that I can call

> chrome

from the command prompt and it will launch Chrome.

I know I could add Chrome's containing directory to my path (set PATH=%PATH%<chrome_path_here>;), but since I have a few executables I want to add, I'd rather make a new bin directory that contains symbolic links to the actual executables and just add that single directory to my PATH.

The Chrome executable is located at

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

So I tried

> mklink chrome.exe "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

That successfully creates a symbolic link for the files (says so in output, and upon examining with > dir). I know my PATH is set up correctly, b/c when I run > where chrome it finds my new symbolic link.

However, when I try to execute chrome with my new link, nothing happens. A new empty window should appear, but nothing happens. No error message in the command prompt or anything.

What am I doing wrong? Am I misunderstanding symlinks in Windows? This is the approach I use in Linux all the time, but I'm new to Windows Cmd.

Thanks!

回答1:

Most programs will not run from places other than they install location - which is exactly what happens when you try to run it from symlink.

It would be much easier to create CMD/BAT files in that folder with matching names which will launch programs from locations you want:

REM chrome.cmd
start /b cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %*


回答2:

With Windows 7 I confirm that symlinks do not work, are simply ignored as reported in the original question.

As Harry states in his comment, shortcuts do work, and to me are simpler and easier than writing a separate script for each new command I want to enable under CMD.

He states that you need to add .lnk to your PATHEXT variable in order to do this. I affirm that this does work, and with .lnk added to PATHEXT I can simply enter the name portion of the shortcut to run the command. For example if my shortcut is named "sublime.lnk" and PATHEXT includes .lnk, I can execute the link with the simple command "sublime". Nice!

As an alternative I found that PATHEXT need not be modified if I simply type in the full name of the shortcut, including the .lnk, at my CMD prompt. E.g., I created a shortcut named "sublime.lnk" under %HOMEPATH%/bin, pointing to "C:\Program Files\Sublime Text 2\sublime_text.exe".

Now by placing %HOMEPATH%\bin in my %PATH% and I can sublime via the command "sublime.lnk".

Either of the above are the best way I know of giving access to various commands from around Windows' filesystem from a CMD prompt. I'm not a Windows expert though, and welcome a better or more standardized solution to this problem.


P.S.: I just found out the hard way that you need to ensure the "Start in:" property of any shortcut you use in this fashion is blanked out, or your program will not start in the directory you invoke the shortcut from.


P.P.S.: On a related note, I discovered how to run Windows Explorer (or its replacement) on the directory your CMD session is logged in to: start ..



标签: windows shell