Question

i want to run git pull in a path for all projects, so i wrote a bat file.

@echo off
setlocal enabledelayedexpansion
set dir=%1
if "%dir%"=="" (
    set dir=%~dp0
)
for /D /R "%dir%" %%i in (*) do (
    if exist %%i\.git (
        cd /d %%i
        rem no effect
        echo %cd%
        rem git pull
    )
)
pause

but is seems that cd in for loop does not take any effect, i don't know why. can someone help me solve this problem?enter code here

Was it helpful?

Solution

It has effect. But, in batch files, when a block of code (the code enclosed in parenthesis) is reached (the same for a line out of a block), variable reads are replaced with the value of the variable before executing the code in the block. So, when your for command is reached, the read of the %cd% variable is replaced with the value of the %cd% variable before executing the code. This speeds and simplifies execution of the code but generate this kind of problems.

You can enable delayed expansion with setlocal enabledelayedexpansion command, and change the sintax from %cd% to !cd!. This tells cmd that that this variable read should be delayed until execution of the line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top