-2

I want to create a batch file function to look at the server and see if I need to update any programs. the way I have it set now works outside of a

for /l ...

The issues that I'm facing is the fact inside of loop you need to use !var! to get the updated variable value. If you bypass it with the goto function as a sudo for /l it works

Here's the lines of code that isn't working properly

setlocal enabledelayedexpansion
...

Set LocalL1=Example1.exe
Set /a LocalL1.Byte=1234
...

Set Servr1L=Example2.exe
Set /a LocalL1.Byte=1234
...

FOR /L %%A IN (1,1,%ServrList.Count%) DO (
   Set Temp1=!%LocalL%%%A!
   Set Temp2=!%ServrL%%%A!
   Set Temp1=!!Temp1!.byte!
   Set Temp2=!!Temp2!.byte!
)

Actual results

FOR /L %%A IN (1,1,%ServrList.Count%) DO (
   Set Temp1=Example1.exe
   Set Temp2=Example2.exe
   Set Temp1=Example2.exe.byte
   Set Temp2=Example2.exe.byte
)

What I want it to output

FOR /L %%A IN (1,1,%ServrList.Count%) DO (
   Set Temp1=Example1.exe
   Set Temp2=Example2.exe
   Set Temp1=1234
   Set Temp2=1234
)

Let me know if you need any more code but I have to do the "!%LocalL%%%A!" for it to work. and outside of the For /L it works just didn't want to hard code each one

Brett Nelson
  • 113
  • 3
  • 19
  • 2
    Perhaps if you were to reveal what your expectations of this code were, we'd be able to provide a solution. Examples. Variable contents for variables you've not `set` within the code you've provided. Have you verified that the contents of your variables is as you expect (other than the obvious result-of-the-for/l with which you are having problems) – Magoo Apr 16 '18 at 05:53
  • Verified the variables are set and added what I want the code the output – Brett Nelson Apr 16 '18 at 05:59
  • 1
    If I understood correctly, you have something I would call nested variables; refer to [this post](https://stackoverflow.com/a/10167990) to understand how to expand something like that... – aschipfl Apr 16 '18 at 05:59
  • 1
    Inside your `FOR /L %%A ...` use `Set Temp1=!LocalL%%A!` followed by `FOR %%T IN (!Temp1!) DO Set Temp1=!%%T.byte!`. The same for `Temp2` (or you may also use _another_ `FOR /L` loop to vary the index # in `Temp#`). This (_array_) management is explained in the answer linked by aschipfl above... – Aacini Apr 16 '18 at 12:15
  • However, your intended results are wrong. Your code should do `Set /a %LocalL1%.Byte=1234` (that is the same of `Set /a Example1.exe.Byte=1234`) in order to show your results. I suggest you to carefully review what you are trying to achieve... – Aacini Apr 16 '18 at 12:29

1 Answers1

3

This batch code should work although it is unclear what you really want:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem ...

set "LocaleL1=Example1.exe"
set "LocaleL1.Byte=1234"
rem ...

set "ServerL1=Example2.exe"
set "ServerL1.Byte=4321"
rem ...

set "ServerList.Count=1"

for /L %%A in (1,1,%ServerList.Count%) do (
   set "MyLocaleLabel=!LocaleL%%A!"
   set "MyServerLabel=!ServerL%%A!"
   set "MyLocaleByte=!LocaleL%%A.Byte!"
   set "MyServerByte=!ServerL%%A.Byte!"
   set My
   echo/
)

endlocal

Windows command interpreter first replaces %%A during loop execution of the command block by current loop variable value. Then SET is executed with delayed environment variable expansion resulting in assigning the value of the environment variable LocaleL1 to environment variable MyLocaleLabel, value of ServerL1 to MyServerLabel, value of LocaleL1.Byte to MyLocaleByte, and value of ServerL1.Byte to MyServerByte.

The values of the four environment variables starting with My in name are output next. So the posted batch file outputs on running it from within a command prompt window:

MyLocaleByte=1234
MyLocaleLabel=Example1.exe
MyServerByte=4321
MyServerLabel=Example2.exe

I changed all the environment variable names because the original names in question are too confusing for me.

Hint: Do not use an arithmetic expression to assign a value to an environment variable. Environment variables are always of type string.

On using an arithmetic expression with set /a LocalL1.Byte=1234 Windows command interpreter has first to convert the string with the hexadecimal bytes 31 32 33 34 00 to a 32-bit signed integer with value 1234, hold in memory with the four hexadecimal bytes D2 04 00 00 (LSB to MSB), and next convert the integer back to a string with the five bytes 31 32 33 34 00. That does not make much sense although done so fast that nobody will ever notice a difference to set "LocalL1.Byte=1234" in execution time on which just a simple string copy is executed (if at all).

Further run set /? in a command prompt window and read the output help carefully from first to last page for command SET. Environment variables can and should be in general referenced within an arithmetic expression by just their names without usage of % or !, i.e. set /A TCount=TCount + 1 or even shorter set /A TCount+=1. The help explains why this works in an arithmetic expression any why this syntax is better.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?

And I suggest to read also

Mofi
  • 46,139
  • 17
  • 80
  • 143