Adding an .exe to default path without adding the directory

22

9

For example, I want to add notepad++ to my PATH, however the directory also contains uninstall.exe and several other files/executables and I don't want them to "pollute" my path. Can I just add the one notepad++.exe?

Louis Rhys

Posted 2011-01-03T14:53:29.760

Reputation: 1 543

Answers

17

You can add a batch script to a directory that is in your path, that looks something like this:

@echo off

:: Notepad++ execution

if [%1]==[-h] goto :HELP
if [%1]==[--help] goto :HELP
if [%1]==[/?] goto :HELP
goto :START

:START
start "" /i "%ProgramFiles(x86)%\notepad++\notepad++.exe" %*
goto :EOF

:HELP
echo -------------------------------
echo Notepad++ Command Argument Help
echo -------------------------------
echo Usage :
echo.
echo notepad++ [--help] [-multiInst] [-noPlugins] [-lLanguage] [-nLineNumber] [-cColumnNumber] [-xPos] [-yPos] [-nosession] [-notabbar] [-ro] [-systemtray] [-loadingTime] [fullFilePathName]
echo.
echo     --help : This help message
echo     -multiInst : Launch another Notepad++ instance
echo     -noPlugins : Launch Notepad++ without loading any plugin
echo     -l : Launch Notepad++ by applying indicated language to the file to open
echo     -n : Launch Notepad++ by scrolling indicated line on the file to open
echo     -c : Launch Notepad++ on scrolling indicated column on the file to open
echo     -x : Launch Notepad++ by indicating its left side position on the screen
echo     -y : Launch Notepad++ by indicating its top position on the screen
echo     -nosession : Launch Notepad++ without any session
echo     -notabbar : Launch Notepad++ without tabbar
echo     -ro : Launch Notepad++ and make the file to open read only
echo     -systemtray : Launch Notepad++ directly in system tray
echo     -loadingTime : Display Notepad++ loading time
echo     -alwaysOnTop : Make Notepad++ always on top
echo     fullFilePathName : file name to open (absolute or relative path name)
echo.
goto :EOF

:EOF

You can name it notepad++.cmd. The help section allows you to easily get information on the switches.

I put all such scripts and command line programs in a directory which is added to %PATH%: C:\Users\Public\Command\ ...and that directory is synced to all computers and virtual machines.

paradroid

Posted 2011-01-03T14:53:29.760

Reputation: 20 970

9

Create a batch file with contents like the following:

@"C:\Program Files\Git\bin\git.exe" %*

This should be saved as a .bat file, such as git.bat in a directory that's in your PATH.

@ suppresses echoing the command to the invoking shell. The quotations "" prevent white space being interpreted as argument delimiters. %* pastes any arguments to the batch file, to the quoted executable instead.

You can now invoke the executable using the part of the batch file name before .bat. In my case, git.

References:

Matt Joiner

Posted 2011-01-03T14:53:29.760

Reputation: 825

For many programs that will hang up CMD until you close it. You need to use start to avoid that, as shown in my answer. – paradroid – 2012-07-03T19:15:05.133

@paradroid: Thanks, but it's standard terminal behaviour to remain attached to the parent unless asked. The user should do start git rather than have it done for them. The batch script would act better if it execd into the requested process but this isn't possible on Windows: http://serverfault.com/questions/315077/is-there-a-windows-cmd-equivalent-of-unix-shells-exec. I could prepend call but it would be pedantic and mostly pointless.

– Matt Joiner – 2012-07-03T20:46:25.557

3

Drag a shortcut to notepad++.exe to C:\Windows\System32.


Alternatively, as suggested by @Synetech inc., you could place your shortcuts in a separate directory (e.g. C:\Shortcuts), and then add that directory to %PATH%:

setx PATH "%PATH%;C:\Shortcuts"

As some of the comments indicate, this only works from the Run dialog. In order to get the shortcuts to launch from a command prompt, you need to add the shortcut extension (.LNK) to your PATHEXT environment variable.

setx PATHEXT %pathext%;.LNK

References:

How can I start applications easily with the Run dialog box?

Mateen Ulhaq

Posted 2011-01-03T14:53:29.760

Reputation: 3 207

1This is the preferred solution (as opposed to a batch file); create a shortcut to it in a (different) directory in the path—don’t pollute Windows’ directories! I have a folder specifically for this purpose called Shortcuts. – Synetech – 2011-05-25T01:46:01.783

1That's the wrong syntax for setx. You do not use an equals sign, and IIRC, you need to enclose the whole path in quotes, if the current %PATH% has spaces in it (which it will). – paradroid – 2011-05-25T02:51:57.390

And doesn’t SETX use tildes for variables instead of percents to avoid expanding them? I know that some env-var setting tool does… – Synetech – 2011-05-25T03:01:33.143

@Synetech: No, it doesn't. You could use bangs when using SETLOCAL ENABLEDELAYEDEXPANSION when you do not want percents expanded in a batch file though. – paradroid – 2011-05-25T03:24:46.487

Well something does, maybe setenv or something (though I’m pretty sure it was a version of setx); I’ll check my drive. Ah, I thought so; it was indeed an older version of SETX.

– Synetech – 2011-05-25T03:43:12.187

@Synetech: setx /? says that you can still use tildes (they do get expanded), but I never noticed that before, and I'm sure that the syntax has not changed since the version in the Windows Server 2003 Resource Kit (which is probably the same version now included as standard). – paradroid – 2011-05-25T04:04:06.293

Well there you go; multiple ways to suit the situation. (Though frankly, I find that using the command-line always results in a potential for problems due to escaping no matter what escape character you choose (eg you can’t have strings that contain ‘%’ and/or ‘!’ and/or ‘~’, etc.) It just can’t be like say, C++ strings where you can create any string, including ones that contain the escape character.) – Synetech – 2011-05-25T04:21:37.223

2This doesn't work. By default (I'm dragging git.exe), "git - Shortcut", renaming this to combinations of "git.exe", "git", "git.cmd" etc. has no effect. It won't acknowledge the shortcut. – Matt Joiner – 2012-07-03T17:58:49.167

3This only works when invoking from the Run As dialog, it didn't work from the command prompt. – Matt Joiner – 2012-07-03T18:41:41.377

1

Just create a symbolic link using mklink in a folder in the Path.

e.g. (in Administrator command prompt)

mklink "C:\Users\Me\Documents\Paths\np.exe" "C:\Program Files (x86)\Notepad++\notepad++.exe"

Luigi Plinge

Posted 2011-01-03T14:53:29.760

Reputation: 159

0

The App Paths registry key does exactly this: set the path to notepad++.exe to "\program files\..." and you'll be able to launch it from Start-Run, cmd, ... just like any executable from a directory in %PATH%.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

Reference: https://stackoverflow.com/questions/4822400/register-an-exe-so-you-can-run-it-from-any-command-line-in-windows

patraulea

Posted 2011-01-03T14:53:29.760

Reputation: 171

This will not make it executable from cmd - only from start-run, or in cmd you have to prefix it with "start" as in "start myapp.exe" – Tahir Hassan – 2015-01-23T11:09:05.027

The Command Prompt this, agreed, lacks the ability to recognize this variable. PowerShell does recognize it. This solution, more importantly, only applies to the directory, and hence, off topic. – Todd Partridge – 2017-05-31T20:04:14.127

0

I didn't like any of the solutions presented here, so I tried something else.

  1. Install link shell extension (more info here)

  2. Create a folder somewhere to store all your shortcuts like Marteen suggests and add it to your %PATH%. I created C:\Shortcuts and used Rapid Environment Editor to add it to my PATH.

  3. Right-click the file you want to add to your path (in Explorer) and click "Pick Link Source"

  4. Right-click in folder you just created and click Drop As > Symbolink Link.

  5. Rename the symbolink link if you want.

That's it. You should be able to access your program via the command-line now. If you just added the new folder to your PATH, you will have to restart cmd.exe or Cmder or ConEmu or whatever it is you use. After that, you can add new programs without restarting.

mpen

Posted 2011-01-03T14:53:29.760

Reputation: 9 109