Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CMD Batch file with FOR loop closes instantly despite having PAUSE command

I have this batch file (GitRepoUpdate.bat) for running git pull command with FOR loop in the GitDev directory using Command Prompt.

@ECHO OFF
:: This batch runs ' git pull ' command for all repos in C:/GitDev folder.

title Git Pull Batch
ECHO Running Git pull in every folder in GitDev

FOR /D %G in (C:\GitDev\*) Do cd %G & git pull & cd ..

ECHO Batch finished its job.
PAUSE

I saved it as a .bat file and when I try to run it (even as an Administrator) it just opens and closes instantly.
Yet, when I delete the code part of the batch:

FOR /D %G in (C:\GitDev\*) Do cd %G & call git pull & cd ..

it opens correctly and shows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Running Git pull in every folder in GitDev
Batch finished its job.
Press any key to continue . . . 

It may be me not knowing how it works but I guess the PAUSE at the end should stop the command prompt from closing…
Any ideas?

EDIT:
I forgot to add that if I paste this line:
FOR /D %G in (C:\GitDev\*) Do cd %G & git pull & cd ..
to the Command Prompt (at any location) it works properly and does its job.

>Solution :

Batch files need an extra % on variables for reasons I can’t recall.
Try FOR /D %%G in (C:\GitDev\*) Do cd %%G & call git pull & cd .. instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading