2

I need to go to a program directory (e.g. git). where git | cd does not work, because it returns a file instead of folder. What is the correct way to achieve that?

3
  • Firstly you have your cd and where git around the wrong way. You need to find out where git is and pipe that into cd, at the moment you are piping the output of cd (which with no options would just tell you the current directory) into the where command.
    – Mokubai
    Apr 13, 2015 at 11:42
  • 1
    He means, where git|cd.
    – krowe
    Apr 13, 2015 at 13:21
  • 2
    cd does not accept input via pipe. It also never has.
    – Daniel B
    Apr 13, 2015 at 17:27

3 Answers 3

3

Here are two ways to do it from a CMD file:

for /f "delims=" %%i in ('where %1') do cd /d "%%~dPi."

or you can eliminate the WHERE command altogether, since cmd has a similar feature already built in:

cd /d "%~dP$PATH:1."

Put the preferred line in a CMD file in your path, like FindExe.cmd, and then just run findexe calc.exe. If you want to run it interactively from the command line (not in a CMD file), I think you're stuck with:

for /f "delims=" %i in ('where calc.exe') do cd /d "%~dPi."

.. substituting calc.exe with the appropriate command.

In case you're wondering, the way you attempted doesn't work for two reasons:

  1. WHERE returns the full path including the file name. You don't want the file name.
  2. cd does not accept piped input.
8
  • Do you have a reference for using $PATH with for?
    – DavidPostill
    Apr 13, 2015 at 17:39
  • 2
    It's in the help for the for command toward the end. (for /?) I suspect it works for other variables as well besides PATH. I skipped the FOR command altogether on that one, but the syntax is the same. It may not be clear from my example, but "%~dP$PATH:1 is operating on the %1 argument passed to the CMD file. I'm removing the mention of FOR in that part of the answer. Apr 13, 2015 at 18:24
  • @GuitarPicker that is very interesting, but could you clarify how you get that $PATH line to work with %1 see pastebin.com/raw.php?i=HEviVCLm where it doesn't work with %1 so how did you get it to work?
    – barlop
    Apr 13, 2015 at 19:22
  • @barlop You have $~dP$PATH:1 instead of %~dp$PATH:1 (leading $ instead of %
    – DavidPostill
    Apr 13, 2015 at 20:00
  • 2
    Neither of your variants will work if you are currently on a different drive to the one where the program is located. Use cd /d as pointed out by @barlop
    – DavidPostill
    Apr 13, 2015 at 20:09
1

How do I go to a program directory?

From the comand line:

for /f "usebackq tokens=*" %a in (`where git`) do %~da && cd %~dpa

From a batch file:

for /f "usebackq tokens=*" %%a in (`where git`) do %%~da && cd %%~dpa

As per the comment from barlop, the do part can be simplified from %%~da && cd %%~dpa to cd /d %%~dpa.


Further Reading


5
  • @DavidPostill You can improve that do cd /d %~dpa but the combination you have %~da && cd %~dpa is good and interesting.
    – barlop
    Apr 13, 2015 at 18:52
  • @PHPst It'd be ideal if CMD supported something like CD expand(where git) But it doesn't, and CMD instead has this very long command that can be used to set a variable to the output of a command. for /f %f in ('echo abc') do set a=%f But you don't want CD blah\bleh\git.exe that won't work So if you have David's for line, and after the 'do' you put cd %~dpa then it does CD to just the path.. and cd /d just makes cd work better, so if the path is on a different drive/partition then the cd will still work. If you do cd z:\blah it won't work unless you do cd /d z:\blah
    – barlop
    Apr 13, 2015 at 19:09
  • @PHPst but anyhow, the primary reason for the complexity. and most of the complexity, is the big for line, and that's because as mentioned, CMD doesn't support anything like cd 'command' and its alternative (for) is a rather laborious exercise in absurdity in length and uglyness and some trickyness. 'For' has a number of uses, it's a weird thing.
    – barlop
    Apr 13, 2015 at 19:24
  • 1
    Out of interest, is there a reason for your preference in using usebackq? (as opposed to not doing usebackq, and using single quotes).
    – barlop
    Apr 13, 2015 at 20:11
  • Let us continue this discussion in chat.
    – barlop
    Apr 13, 2015 at 20:23
0

using for modifiers only (quicker, more compact and also works if where is not available) and pushd so it's possible to go back to origin:
for %i in (git.exe) do pushd %~dp$PATH:i

(I just noticed GuitarPicker proposed similar, but not exactly the same, solution...)

You must log in to answer this question.

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