3

This ended up being a path issue. I was using "path" as one of my variables elsewhere in the script without realizing that this actually edited the environment variable PATH.

Original question:

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

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

EXIT

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

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

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

If it's due to a permissions issue (like can't execute another git command while one is in progress sort of thing), any idea how I might make this script work (preferably without having to make a temporary file)? Or is making a temp file for the output of the first command the only way?

Lectrode
  • 412
  • 1
  • 3
  • 13

3 Answers3

2

It should be a PATH issue, though.

The OP Lectrode confirms:

It was a path issue.
I just realized that in my original script I was using path for one of my variables.
I did not realize that was setting the environment PATH variable.

Apparently git uses that "batch proxy" thing.
The command works when you change git status to CALL git status.


Original answer:

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

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

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

And it did returned, when executed within a Git repo on Windows:

C:\prog\git\tests\my_repo>..\c.bat
PATH=Z:\apps\git176\bin;...
Z:\apps\git176\bin\git.exe
 with param  origin/master
Z:\apps\git176\bin\git.exe
# On branch master
nothing to commit (working directory clean)
 with param
Z:\apps\git176\bin\git.exe
# On branch master
nothing to commit (working directory clean)

So it seems to work. (As pointed out by Magnus, I don't use exit)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thx for your input. Running the code you provided (minus the 'which' commands and the label note) results in ` with param -branch name- The system cannot find the batch label specified - myDosFunc` which again is odd because if it couldn't actually find it, it wouldn't process the `echo.with param` at all. I may just have to re-install windows. Something isn't right. Using `EXIT` or `GOTO :EOF` doesn't make a difference. – Lectrode Mar 22 '12 at 13:51
  • @Lectrode: regarding your error message, would the following SO questions help? http://stackoverflow.com/q/1522129/6309, http://stackoverflow.com/questions/232651/why-the-system-cannot-find-the-batch-label-specified-is-thrown-even-if-label-e – VonC Mar 22 '12 at 14:50
  • Ugh, it was a path issue. I just realized that in my original script I was using `path` for one of my variables. I did not realize that was setting the environment PATH variable. Thanks for helping me realize this error @VonC Thanks, that helped. Apparently git uses that "batch proxy" thing. The command works when you change `git status` to `CALL git status` – Lectrode Mar 22 '12 at 16:47
  • @Lectrode: interesting. I have included your conclusions in the answer for more visibility. – VonC Mar 22 '12 at 16:54
0

I suspect it has to do with the EXIT. Does git status work before that line?

rtn
  • 127,556
  • 20
  • 111
  • 121
  • Replacing `EXIT` with `GOTO :EOF` does not make a difference. All of my other batch files have `EXIT` before most of the code, and they run fine. Thx for input tho – Lectrode Mar 22 '12 at 13:53
0

My colleague said the following:

I am pretty sure the comand interpreter is interpreting the whole command as a command including the spaces. The single quotes look dangerous to me, better try double quotes.

And Use a real scripting language, for Windows PowerShell would be the choice nowadays.

I tested the script, what does not work actually is the CALL :somelabel , it says it does not know somelabel. The git commands get executed fine, both of them Win 7 / x64, and a decently current git (1.7.

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 1) There's a reason for the single quotes. Double quotes would treat the contents like a string instead of a command. The spaces are part of the command. 2) Powershell might be newer and have more functionality, but for a quick script that easily runs on more than just your computer, BATCH is still the way to go. If I need that functionality, I'd rather use c#. I can't just hand a non-technical friend a powershell script and expect them to know how to use it. Also, this is not the place to discuss this anyway. 3) `CALL :somelabel` was not the issue (look at answer) Thx for ur input tho =) – Lectrode Mar 23 '12 at 02:19