0

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

Ben E.
  • 10,550
  • 4
  • 15
  • 10
  • Works fine for me. Escaping is only necessary between `('` and `')`. There is no output to be expected with your second snippet. `numSlashes` gets populated with the string `something`. How did you determine that it "does not work"? – Stephan Mar 03 '23 at 12:22
  • My assumption is that they're using, but not showing, the variable `%numSlashes%` instead of `!numSlashes!`. Essentially a longstanding account holder using the site without submitting a [mcve], and expecting others to guess the problem code they are having an issue with! Although the way the code is written, we also have to assume that `something` is a string containing `%%A` or a modifier of it `%%~…A` – Compo Mar 03 '23 at 14:14
  • @Stephan after the batch file has run, I run `set numSlashes` inline in the console window, and the response is that environment variable numSlashes is not defined – Ben E. Mar 03 '23 at 19:22
  • @Compo I'm happy to include the whole script, but I want to avoid this devolving into a "fix my script for free" question (by the way I really appreciate your time!). See my last comment, I deemed it not to work by running `set numSlashes` afterwards. Can you reproduce this now? – Ben E. Mar 03 '23 at 19:29
  • 1
    When the script ends, there is an implicit `endlocal`. Everything you defined after a `setlocal` ceases to exist when the script ends. You can "rescue" a variable with `endlocal & set "numslashes=%numSlashes%"` before exiting the script. – Stephan Mar 03 '23 at 21:32
  • I never asked for a full script @BenE. I shouldn't need to tell you that we require a [mcve]. You've not submitted one, because information you've used in your comments does not appear in your question or code, and therefore does not fulfil the requirements. – Compo Mar 03 '23 at 21:39
  • @Stephan Thanks for the tip about setlocal scope - thought that setlocal was a means to setting EnableDelayedExpansion - thanks for explaining what I'd missed! That said, still no dice for me, somehow. I've updated the description with a simple test of a variable alive within the setlocal scope, but the echoed output doesn't contain the value of the variable. I've also added a second problem with cd. Could you check these out please? – Ben E. Mar 04 '23 at 09:09
  • 1
    Problem2: `@echo directory is now !CD!` - [delayed expansion issue](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Mar 04 '23 at 09:10
  • @Compo Sorry about that, in my naivety I assumed a comment would suffice, but as you say the standard clearly states it's got to be up top. I have updated the question and code, please check it again if you have time :) FWIW, I felt the way you characterised me in your first comment was unproductive, it did not move us closer to an answer to the question - what are we here for if not for that. As an infrequent poster I don't know the rules inside-out, but did appreciate your link to MRE standard. Hopefully it's plain sailing from here. – Ben E. Mar 04 '23 at 09:32
  • @Stephan looks like the excalamation fixes Problem 1 as well - what a legend! What's protocol now, do you create an answer with these details, or do I close the question? – Ben E. Mar 04 '23 at 09:41
  • 1
    closed as duplicate (that's the standard way here. Makes no sense to duplicate the answer again and again) – Stephan Mar 04 '23 at 09:43

0 Answers0