10

I tried searching this on Google and didn't really learn anything as the search results usually pertain to other recursive subjects. What I would like to know is if a folder is in Path is it defined recursively (on Windows)?

I want to create a C:\StandalonePrograms and add that to path. It will contain a bunch of programming languages and other programs that usually come from zip files. I want to know that if by adding the program directory to it I can call all of the programs.

For example if I have C:\StandalonePrograms\SomeProgram can I open up a command prompt type someCommand and expect it to run from the C:\StandalonePrograms\SomeProgram\bin folder?

Or do I need to explicitly define C:\StandalonePrograms\SomeProgram\bin in my Path?

If I can't are there any workarounds to achieve the situation I want?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Rumel
  • 917
  • 1
  • 9
  • 21

2 Answers2

11

You need to specify each directory individually, the PATH mechanism doesn't walk through subdirectories.

A workaround could be a directory full of batch files (of some sort) that start the real tools with full path

Patrick Georgi
  • 688
  • 4
  • 8
  • 1
    Instead of creating batch files, just add `C:\ProgramData\Microsoft\Windows\Start Menu\Programs` to `%PATH%` and make sure every program has a shortcut there. Then add `.LNK` to `%PATHEXT%` so that you can type `` instead of `.lnk`. – Detached Laconian Apr 20 '19 at 02:24
  • Note that the program will, by default, start in some folder inside `Program Files`, meaning ` arg.txt` will use a file called `arg.txt` inside `Program Files`, rather than the working directory. To fix that, right-click the shortcut, select `Properties` and fill in an empty string `""` for the `Start in` field. Alternatively, follow the instructions here: https://stackoverflow.com/a/8163798/2153942 . – Detached Laconian Apr 20 '19 at 03:02
2

Here is a workaround. Save this as "SetMyPath.bat" (or with another name):

@echo off
set dir=%*
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /s /ad /o:d /b "%dir:"=%"') do set path=%%i;!path!
cmd

(Here, "%dir:"=%" is only needed to permit you to omit quotation marks around the directories with space in the names when calling this file. If you don't need this, then %1 would do instead.)

This file takes one command-line argument: the directory. It will launch a new copy of cmd.exe, where files under the given directory will be available:

C:\> mysqldump.exe
File not found.
C:\> SetMyPath.bat C:\Program Files\MySQL
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\> mysqldump.exe
Usage: mysqldump [OPTIONS] database [tables]
C:\> exit

In this example, the first command shows that mysqldump.exe is not on the path. After you execute the batch file, a new cmd.exe is launched, where mysqldump.exe is available. When you finish working with it, exit returns you to the original copy of cmd.exe.

If there are two copies of the .exe file under different subdirectories, the copy in the most recently updated directory will be launched (because of /o:d). In this example, assuming that the directory of the most recent version of MySQL was updated last, the most recent version of mysqldump.exe will be launched.

The batch file can be modified to guarantee that the most recent copy of the .exe be launched (ask me in a comment if you need it).

Alexander Gelbukh
  • 2,104
  • 17
  • 29