Multiple for loops for setting variables but only first work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OmerHassan
Posts: 2
Joined: 21 Feb 2013 09:58

Multiple for loops for setting variables but only first work

#1 Post by OmerHassan » 21 Feb 2013 10:01

Here's my Windows batch file:

Code: Select all

FOR /F %%a IN ('git rev-parse --abbrev-ref HEAD') DO SET branchName = %%a
FOR /F %%a IN ('git rev-list --max-count^=1 %branchName%') DO SET localCommitId = %%a
FOR /F %%a IN ('git rev-list --max-count^=1 origin/%branchName%') DO SET remoteCommitId = %%a

@ECHO branchName = %branchName%
@ECHO localCommitId = %localCommitId%
@ECHO remoteCommitId = %remoteCommitId%


Here's the output I see when I run it:

Code: Select all

>FOR /F %a IN ('git rev-parse --abbrev-ref HEAD') DO SET branchName = %a

>SET branchName = develop

>FOR /F %a IN ('git rev-list --max-count=1 develop') DO SET localCommitId = %a

>SET localCommitId = e4375fa4753b3956e1454020a812cc7591cf606e

>FOR /F %a IN ('git rev-list --max-count=1 origin/develop') DO SET remoteCommitId = %a

>SET remoteCommitId = e4375fa4753b3956e1454020a812cc7591cf606e

branchName = develop
localCommitId =
remoteCommitId =


I don't understand why localCommitId and remoteCommitId are empty despite the printed SET commands setting them to non-empty strings.

I guess the function of git commands is irrelevant to the question because I get the same problem if I replace them with, say, ver.

OmerHassan
Posts: 2
Joined: 21 Feb 2013 09:58

Re: Multiple for loops for setting variables but only first

#2 Post by OmerHassan » 21 Feb 2013 10:27

Solved. I had to remove spaces from around the assignment operators. For example, I changed branchName = %%a to branchName=%%a.

Post Reply