I am working on a script to automatically pull code changes in a series of Git repositories, which are stored in a folder structure which mirrors the repo they are stored in. The goal is to iterate through the folders, then for each one I will run some git CLI commands. Similar to this post.
The robocopy
part is working well (borrowed from here - it returns a clean list of subfolders with a maximum depth specified in /lev
. But as all the git repositories are stored at EXACTLY /lev
layers deep, for each iteration I need to ignore folders that are less than /lev
layers deep. The script will do this by checking the number of slashes in the folder.
I'm having some problems executing certain commands inside a for loop:
Problem 1: I cannot echo the value of a variable set inside the loop, which effectively prevents me from debugging the most important part of my script. @Stephan has pointed out that variables only exist inside the setlocal scope (thanks!), but for me I'm still not seeing the variable populated:
@echo off
setlocal EnableDelayedExpansion
for /f "skip=2 tokens=*" %%A in ('robocopy . . /l /s /njh /njs /ns /lev:3 ') do (
set "numSlashes=something"
echo value of numSlashes is %numSlashes%
)
endlocal
pause
Problem 2: Change Directory command does not seem to be working either. After running the command cd %%A
, I expect the %cd%
variable to have a new value. But when I @echo
the value of %cd%
, the old value is returned:
@echo off
setlocal EnableDelayedExpansion
for /f "skip=2 tokens=*" %%A in ('robocopy . . /l /s /njh /njs /ns /lev:3 ') do (
@echo directory was %cd% now moving to %%A
cd %%A
@echo directory is now %cd%
)
endlocal
pause
Feels like I am making some basic errors. Can anyone point out what I've done wrong? Thanks