0

Duplicate of Passing a variable in batch file to another batch file with FOR loop to process list of folders

For loop can't access variables with %%. Reason why the code was messed up and didn't work. It's working now. This is just an example on how to access variables within loops. abc.bat

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mnp=90
FOR /L %%i IN (1,1,5) DO (
 SET /a mnp+=1
 CALL def.bat !mnp!
 )
 PAUSE

and def.bat

ECHO %~1 %~2
  • Does nothing to fix that issue above, it still doesn't see that variable at all. I already got to this and that calling subroutines section but unfortunately it still gives me the same output. Can you guys post a successful version of that example code? – Peter Anderson Feb 25 '20 at 02:08
  • what did you expect `cmd /c %1` was going to do for you? replace it with `echo %~1` and see the result then. Once `%cod% becomes an actual batch file or command, you could run it as either `call %~1` or `%~1` or `start "" %~1` If your value is going to change inside of the codeblock however, you would need `delayedexpansion` – Gerhard Feb 25 '20 at 05:51
  • @GerhardBarnard Can you see if the edits I made are according to the suggestion you made? If not, please edit them if possible, or comment here. – Peter Anderson Feb 26 '20 at 15:29
  • What happens when you change `%mnp%` to `!mnp!` – Gerhard Feb 26 '20 at 16:44
  • And please explain why you are usimg a `for /l` loop but not utilising metavariable `%%i` for instance. `for /L %%i IN (1,1,5) do call def.bat %%i "2"` – Gerhard Feb 26 '20 at 16:49
  • It's the same result with `!mnp!`. And to answer your second question, why I am not using a meta variable is because I want to use another variable with a different value. It's not about the meta variable. Funny thing, I tried it with a global variable. It does access it but it doesn't do any functions on it at all. That was just an example. `SETLOCAL delayedexpansion` did not help at all. – Peter Anderson Feb 26 '20 at 18:26
  • Do me a favour. Edit the question and show what you really use. If there is any sensitive data, which I doubt, just replace it with dummy text. – Gerhard Feb 26 '20 at 18:33
  • Okay, sorry. I can't edit the comment due to some browser issue. Anyways, my mistake. `!mnp!` makes the variable accessible. But, I was not able to do any operations on it even with delayedexpansion enabled. The variable is supposed to be local, accessible and available during the for loop only. – Peter Anderson Feb 26 '20 at 18:33
  • Oh it's fine now. Had to use the /a on it for the arithematic operations. In my other code it's doing string operations, and thus I forgot to include that flag. It's doing it now and with !! it's setting the variable fine enough. Seems like %% doesn't work in a for loop to access a variable. – Peter Anderson Feb 26 '20 at 18:45
  • See. If you included what you really used we would have aolved this in the first few minutes. – Gerhard Feb 26 '20 at 18:47
  • It's very confusing to edit the code in a question to a working one and leave the text around that describes a problem that doesn't exist (anymore). I tried for several minutes to find the described problem (or any problem) in your code. Please either delete the question (preferably) or roll it back to the second version (when it actually was a valid question) and add your working code as an answer instead. – Stephan Feb 26 '20 at 18:56
  • As a side note. It is better to use `SET /a mnp+=1` than the current method. – Gerhard Feb 26 '20 at 18:56
  • Perhaps. And I'll do so Stephen when I'm on my desktop. Also, I was indeed using that method only to do the arithmetic operation but since nothing was working, I had to resort to the basics of the basics. Anyways, I am not sure why for and if conditional statements don't access variables with %%. Has C always been like this? Not sure now, haven't touched it in years after school. – Peter Anderson Feb 28 '20 at 02:26
  • It's called [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). `Cmd` does it since the beginning. See [this thread](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) (get yourself a big can of coffee before start reading). – Stephan Feb 29 '20 at 15:22
  • @Stephan It did not have any effect even with delayed expansion on. %% did not. !! did. I was referring to why for loop didn't access any variables with %% but it did with !!. Delayed expansion did not have any effect while I used %% and when I disabled it while using !! to access the vars, it worked exactly how it is said in the documentation for delayed expansion. – Peter Anderson Feb 29 '20 at 18:10
  • Did you read the links, I provided? The whole `for` or `if` construct (aka. "Code block") is parsed once, even before anything gets executed. Every `%var%` gets replaced by its value at this point (therefore `%var%` doesn't change). As that is sometimes not what is wanted (obviously), they "invented" delayed expansion and to differ the delayed variable "type" from the "normal" ones, they had to come up with something different from `%` and chose `!`. They didn't want to treat all variables as "delayed" to keep old scripts compatible and running (that's the problem with evolving languages) – Stephan Feb 29 '20 at 19:11
  • My apologies, I skimmed through most of it the first time. Had to go through all of it again which, yes took a considerable amount of time and I couldn't understand everything in one go. From what I understand then, if I want to create a temp variable inside the For loop, I need to choose ! with delayed expansion on for it to work properly. At least, that's how the variable created within the loop got created and then got accessible in my code. – Peter Anderson Mar 01 '20 at 02:52

0 Answers0