0

I have the below code snippet which outputs a variable in to a text file. %drone% will be a simple text string, and the %counter% is a number which is in a loop to count the iterations.

The echo %drone% !counter! echos the correct values to the screen but in the echo Done: %drone% Files deleted: !tempcounter! >> clearTempFilesonDrones.txt code the !tempcounter! variable is blank when outputted to the file.

I've tried to set it to a temp variable and use %tempvariable%, but that didn't work. I also tried like "!counter!" incase it would think that that's a string instead of just a number, but didn't work either.

echo %drone% %counter%
endlocal

echo Done: %drone% Files deleted: !counter! >> clearTempFilesonDrones.txt

Any ideas?

I can't copy the entire code onto a pastebin or the like as blocked in current client, so just uploaded an extract.

Brian Folan
  • 205
  • 2
  • 9
  • This lacks of information, you should provide more code (please consult [this help topic](http://stackoverflow.com/help/mcve)), and you should describe what you mean by "didn't work"; anyway, there must be `setlocal EnableDelayedExpansion` command somewhere; besides enabling delayed expansion, this begins localisation of environment changes, hence every variables set or changed are lost as soon as `endlocal` ends the localisation; I assume `tempcounter` is set in between `setlocal` and `endlocal`, isn't it? – aschipfl Aug 29 '16 at 14:46

2 Answers2

2

your endlocal ends the delayed expansion, meaning you can't use ! variables afterwards anymore. try simply using

echo Done: %drone% Files deleted: %counter% >> clearTempFilesonDrones.txt

Note that variables which are first accessed with !temp! in a code block can be accessed with regular %temp% afterwards

If that doesn't work I suggest trying to remove the endlocal, or placing it in a line below the last echo.

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • 3
    Note that the `endlocal` _also_ deletes all variables that were modified since the previous `setlocal`; this means that after the `endlocal` the result variables may have different values, even if they are show with `%` instead of `!`. – Aacini Aug 29 '16 at 14:37
  • Did try this and didn't work, and also tried having the endlocal on the next line. My question was actually wrong, in that I had the `endlocal` at the end of the script, not before there. – Brian Folan Aug 30 '16 at 10:06
1

It looks like behavior of setlocal without or with 1 or 2 of the optional 4 parameters and endlocal is not really known by you.

See the answers on

which describe in detail with examples what setlocal and endlocal really do.

The current values of the environment variables drone and counter must be passed from current environment variables table deleted by endlocal to the restored environment variables table active before setlocal and active again after endlocal.

This can be done as follows:

echo %drone% %counter%
endlocal & set "drone=%drone%" & set "counter=%counter%"

echo Done: %drone% Files deleted: %counter% >>clearTempFilesonDrones.txt

The entire command line with the command endlocal and the two set commands is first preprocessed by Windows command processor with the values of current environment variable resulting in replacing the variable references by their current values. So the line is after preprocessing:

endlocal & set "drone=value of drone variable" & set "counter=counter value"

Now first endlocal is executed which discards the current environment variables table and restores previous environment variables table. Then then two set commands are executed which assigns the two strings to the two variables in now active environment variables table.

For an explanation of operator & see the answer on Single line with multiple commands using Windows batch file.

Further I suggest to open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • endlocal /?
  • set /?
  • setlocal /?

The built-in help is not so detailed as the referenced answers, but should be nevertheless read.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • This works... As per a previous comment on another post, I did have the endlocal at the end of the script and moved it up when I tried to set the a new variable to my `%counter%` value. I guess that I needed to do it on the same line. Thank you! – Brian Folan Aug 30 '16 at 10:08