3

From the windows command line, if I am in c:\foo, and c:\bar is on my PATH.
Is there any way to call '\bin\myapp.exe' and have it search my path to invoke c:\bar\bin\myapp.exe?
perhaps a prefix/environment variable or a standard utility?

I believe the above would work fine on a Macos/linux system (?) and works fine in windows if there was no folder information in the command (ie c:\bar\bin is on the path and I only call myappe.exe)

Update - this is for a script that does not know the (third-party) install directory, it only knows that the base directory is on the path (and the exe name is unique enough that it is unlikely to collide with an app with the same name). I could probably write a script to find(grep) it, was hoping there was an easy trick (cmd.exe option, etc) to allow the subfolder name in the command, similar to how unix would allow

2
  • Does the third party install write anything into the registry?
    – DavidPostill
    Commented Sep 21, 2016 at 21:41
  • Unfortunately no
    – bitcoder
    Commented Sep 23, 2016 at 14:35

2 Answers 2

1

Read Command Line arguments (Parameters) or call /?:

…
If Command Extensions are enabled CALL changes as follows:
…
In addition, expansion of batch script argument references (%0, %1,
etc.) have been changed as follows:
…
        %~$PATH:1   -  searches the directories listed in the PATH
                       environment variable and expands %1 to the fully
                       qualified name of the first one found.  If the
                       environment variable name is not defined or the
                       file is not found by the search, then this
                       modifier expands to the empty string
…

The FOR command creates parameter variables which are identified with a letter rather than a number (e.g. %%G). The Parameter Expansions described above can also be applied to these.

Applying above to your case (note doubled percent sigh in %%g for using in a .bat or .cmd script):

for %%g in ("bin\myapp.exe") do @set "_pathToMyApp=%%~$PATH:g"
rem         ↑↑  NO leading backslash
rem next line calls `myapp.exe` using its fully qualified name
"%_pathToMyApp%"

A real example: (note single percent sigh in %g for using from command prompt in an open cmd window). My PATH variable reads as …;C:\Utils\;… with no further reference to C:\Utils folder (base directory), myapp.exe is a simple application which displays all supplied parameters:

d:\bat> myapp.exe
'myapp.exe' is not recognized as an internal or external command,
operable program or batch file.

d:\bat> bin\myapp.exe
The system cannot find the path specified.

d:\bat> set _
Environment variable _ not defined

d:\bat> for %g in ("bin\myapp.exe") do @set "_pathToMyApp=%~$PATH:g"

d:\bat> set _
_pathToMyApp=C:\Utils\bin\myapp.exe

d:\bat> "%_pathToMyApp%" display all parameters
param 0 = C:\Utils\bin\myapp.exe
param 1 = display
param 2 = all
param 3 = parameters

d:\bat>
1
  • This worked great, well thought out. Not 100% what I was hoping the answer would be but gets me around my problem. (unfortunately I do most posting on StackOverflow so my reputation here on superuser is not sufficient to upvote you properly, I need at least 15 rep)
    – bitcoder
    Commented Sep 23, 2016 at 14:53
2

In windows you can set your own environment variables and then use them. So you could go to:

  • Control Panel
  • System
  • Advanced System Settings
  • Advanced tab
  • Environment Variables

Then just click on "new" and add your variable. You can then use that variable in commands. So you could just set your path and that use it as you like with whatever name you give it.

Example:

So in your case you could set the following variable : appNameBinPath = "C:\bar" You could then use it in the following example:

C:\foo>%appNameBinPath%\bin\myapp.exe 

Alternatively if you want to set a permanent variable by script and/or command line, you can see this article: https://stackoverflow.com/questions/5898131/set-a-persistent-environment-variable-from-cmd-exe

2
  • My problem is that my script doesn't actually know the base directory (could be installed in different places) . My original question didn't clarify that though so I have made an update
    – bitcoder
    Commented Sep 21, 2016 at 20:51
  • If the script doesn't know the base directory and You don't know it either to be able to manually set it for all instances in the environment variables, you won't have a choice to have a recursive function that will look through the directories to find it. This could get messy though unless you specify a maximum number of recursions. Commented Sep 21, 2016 at 20:59

You must log in to answer this question.

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