33

I've got a directory in my home folder in which I place command-line software (CMD_Software). I put each piece of software in its own directory within CMD_Software in order to avoid clutter.

I would like to make a single entry in my PATH variable that will allow access to CMD_Software and all directories it contains from the command line.

I tried C:\Users\myuser\CMD_Software\* but that did nothing. That's the point at which I ran out of ideas.

Please note that I'm not trying to set a new path while in the terminal, I'm trying to set a new path in the "Environment Variables" available on the "Advanced" tab of System Properties.

2
  • Did you try something like `C:\Users\myuser\CMD_Software*`?
    – terdon
    Mar 21, 2013 at 16:18
  • 5
    I don't think this is possible. Mar 22, 2013 at 2:22

4 Answers 4

25

The PATH variable does not support wildcards or recursion. This is by design.

There are two possible workarounds that I've used on occasion:

  • Create a directory with simple batch files and add that directory to the PATH. Each batch file can launch the program you want, for example:

    :: CMD_Software.bat: start CMD_Software
    @C:\Users\myuser\CMD_Software\CMD_Software.exe %*
    

    The first line is a comment, the second starts with @ to avoid showing the command being run, and %* is used to pass any command line arguments to the EXE.

  • Add aliases to CMD.EXE:

    DOSKEY CMD_Software="C:\Users\myuser\CMD_Software\CMD_Software.exe" $*
    

    This essentially translates CMD_Software in the command prompt to everything after the equal sign. The $* is replaced with the supplied arguments.

I prefer the second approach, because you can group all the aliases in a single file (see the "/MACROFILE" switch in DOSKEY /?) and have it autorun whenever the command interpreter starts using a registry setting (see "AutoRun" key in CMD /?).

A drawback of the second method is that aliases work only at the start of a command line. This can be a problem if you want to chain commands. For example, CLS & CMD_Software won't work unless you put the alias in a separate line using parentheses:

CLS & (
CMD_Software
)

Whenever this becomes a problem, I just fallback to the batch file approach.

2
  • Thanks! The first method works really well for me, mostly because I don't have to mess with registry when I transfer my data between computers. It's also pretty tidy and convenient since you only have spend a tiny bit more time to create a bat file when you get said utility.
    – cyqsimon
    Jul 24, 2019 at 21:40
  • Unfortunately I have to redact my previous statement this quickly. I have encountered strange issues with scripts randomly exiting half way through execution after adopting the first method. In particular, wget for Windows seems to enjoy killing my script after it has finished downloading. I have wasted last two hours painfully troubleshooting my script, but eventually I tried throwing all the exes into a single root directory, and the problems just all disappeared. Therefore I would strongly advise caution before anyone goes all in with this method.
    – cyqsimon
    Jul 25, 2019 at 2:12
5

This is what I use to fix the problem. Copy this script below and save it as FIXPATH.BAT into the folder that you've added to the PATH environment variable. For instance:

C:\Users\myuser\CMD_Software\

Now whenever you need to run a program that's in a subfolder, say...

C:\Users\myuser\CMD_Software\unixutils\grep.exe
or
C:\Users\myuser\CMD_Software\imagetools\exiftool.exe

Run fixpath first then enter the command (e.g. grep or exiftool) like so:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\myuser>fixpath
C:\Users\myuser>exiftool


FIXPATH.BAT

setlocal EnableDelayedExpansion
cd /D %~dp0

for /R %%d in (.) do (
    set "dirs=!dirs!;%%d"
)

path=%path%!dirs!
(endlocal
    set "ret=%PATH%"
)
cmd /K "title [#] Path Fixed [#] && path %ret%"
exit /B

Thanks to @VonC's answer

1

Put a "programlinks\" subdirectory under ...\CMD_Software (or anywhere, actually) and fill it up with symbolic links or hardlinks to each executable you wish to access. Then a single entry in the PATH for ...\programlinks will suffice.

3
  • 1
    The main problem with that is that if there are any libraries that the executable depends on located potentially in the same directory as the exe, they won't be used. Or at least that's what's happening with my attempt with this.
    – zero298
    Feb 13, 2015 at 20:43
  • Great remark, @zero298. Although I'd like, if anyone is 100% sure and can confirm this is actually true to please chime in. (Cause this method is otherwise the absolute cleanest of all.)
    – Henrik
    Dec 18, 2019 at 1:02
  • 1
    I use a deviation of this that solves the problem @zero298 mentioned. If you create shortcuts (.LNK) files you can specify the directory it starts in. All you need to make this work is to modify your PATHEXT variable to include .LNK. In fact, I have .LNK at the front of the list, so any shortcuts in my PATH variable get resolved first. To boot, you can also specify command parameters in the shortcut as well, as per normal functionality. Jul 27, 2020 at 13:03
-2

If you're not too put off by the idea, you can copy your command line tools to the Windows\System32 directory. Then you can run them from any directory, just like ping or ipconfig.

edit
After a bit of tinkering around, this IS possible. You just need to use semicolons to delimit your directories. Say you had a program in "folder1" and "folder2". You'd write your path like this:

c:\folder1\;c:\folder2\;c:\folder3\;    etc....

Then you can call a program in either directory straight from the command line. AFAIK there is no other way to do this with just single line.

Supporting info: http://en.wikipedia.org/wiki/PATH_(variable)

1
  • 2
    This doesn't make a lot of sense. 1. This would erase any previous content of the PATH variable. 2. The PATH variable would only contain the folder, not its subdirectories (desired result). 3. Invoking program.bat as %PATH%\program.bat is more or less the opposite of what PATH is designed for.
    – Dennis
    Mar 28, 2013 at 0:07

You must log in to answer this question.

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