"git" is not recognized as internal "blah blah" even though the command works the first time

This turned out to be a problem. I used "path" as one of my variables elsewhere in the script, not realizing that this is actually editing the PATH environment variable.

Original question:

I am writing a batch file. In a batch file, I have the following:

FOR /F "tokens=*" %%a IN ('git branch -r') DO CALL :SOMELABEL %%a


git status
START /B /WAIT CMD /C git status
FOR /F "tokens=*" %%n IN ('git status') DO ECHO %%n


'git status' is just an example. Each of the 3 'git status return' 'git' commands is not recognized as an internal or external command, operating program, or batch file. "

This will usually be an environment variable (PATH) issue, but what makes this unique is that the first git command ("git branch -r") works. I have maybe 5 more batch files that can use the git command successfully. Any ideas as to why the later git commands won't work in the code above?

If it is due to a permissions issue (e.g. cannot execute another git command while something is happening in it), any idea how I can make this script work (preferably without the need for a temporary file)? Or makes a temporary file for the first command output the only way?


source to share

3 answers

This must be the problem PATH


OP Lectrode confirms:

This was the way.
I just realized that in my original script, I was using for one of my variables. I didn't understand what was setting the environment variable . PATH


Apparently git uses a batch proxy . The command works when you change git status

to CALL git status


Original answer:

I just tested the following script successfully, following the syntax outlined in the DOS Batch - Function Tutorial section (and using the 'which' command I installed via gow ):

@echo off
which git.exe
FOR /F "tokens=*" %%a IN ('git branch -r') DO CALL :myDosFunc %%a

:myDosFunc    - here starts my function identified by it`s label
echo. with param  %~1
which git.exe
git status


And it returned when executed in a git repository on Windows:

 with param  origin/master
# On branch master
nothing to commit (working directory clean)
 with param
# On branch master
nothing to commit (working directory clean)


So it works. (Since pointed to Magnus , I am not using exit




I suspect it has to do with the exit. Is git state running up to this line?



My colleague said the following:

I'm pretty sure the comand interpreter interprets the whole command as a command, including spaces. Single quotes look dangerous to me, better try double quotes.

And to use a real scripting language, Windows PowerShell will now have a choice.

I tested a script that doesn't actually work CALL: somelabel, it says it doesn't know somelabel. The git commands run great, both are Win 7 / x64 and a decently current git (1.7.



All Articles