Page 1 of 1
Confusing Variables
Posted: 09 Jul 2014 22:17
by RightBehindu
Hey guys.
I am not really sure how to explain what I need help with, but I will try. I have an item, and the values for this item is stored in a file. But, I want the details to be completely manipulated by the file. I want to change one variable in the file, but keep the others the same. I am sure there is a very easy way to do this, and if there is please explain

but I was thinking along the lines of like so:
Code: Select all
(
echo set %itemname1%=%itemname1%
echo set %itemname1%value=%%itemname1%value%
echo set %itemname2%=%itemname2%
echo set %itemname2%value=true
) > Datafile.bat
But as you can see, I don't think you can have more percent signs in a variable. It is really hard for me to explain, but I just want to, lets say, change item 2 to true, but keep all the details of item 1 the same. Sorry if I am being to vague, but I am really confused myself.
Thanks if you can understand what I mean.
Matt
Re: Confusing Variables
Posted: 10 Jul 2014 02:57
by RightBehindu
Just happened again. Same problem. I need to echo a specific key to a file, and I have set for example:
and I want to:
Code: Select all
set /a filenum=%filenum%+1
pause
echo set %file%filenum%% >> %file%filenum%%.bat
but all I get out is filenum%
Re: Confusing Variables
Posted: 10 Jul 2014 06:48
by Squashman
Not sure what you are trying to do with that echo statement as all it will do in the 2nd batch file it just display the variable name and what it is set to.
Not sure if this is the desired output you want or not.
Code: Select all
setlocal enabledelayedexpansion
set file1=AA-AA-AA-AA
set filenum=0
set /a filenum=%filenum%+1
echo set !file%filenum%! >> !file%filenum%!.bat
pause
Re: Confusing Variables
Posted: 10 Jul 2014 07:35
by RightBehindu
That is indeed what I want to do, although it wouldn't work for me. What exactly does the setlocal and EnableDelayedExpansion do?
Re: Confusing Variables
Posted: 10 Jul 2014 07:56
by Squashman
RightBehindu wrote:That is indeed what I want to do, although it wouldn't work for me.
Are you saying that the code didn't work or this type of code is not what you want?
Re: Confusing Variables
Posted: 10 Jul 2014 08:09
by aGerman
Nested variables is what you should look for via Google.
The reason why your example doesn't work is because it is parsed as follows
%file%filenum
%%There are two workarounds (in simple terms).
1) CALL expansion
call echo %%file%filenum%%%
...where double percent signes are reduced to one and %filenum% is expanded at parse time
the CALL lets the resulting variable (eg.
%file0%) expand to it's value
2) Delayed expansion
Normal variables are expanded at parse time. That means variables will be replaced with their values
before a command line (or a block of command lines enclosed into parentheses) is executed. To avoid this behavior you can create a sub-environment (SETLOCAL) with delayed variable expansion enabled. Thus, a variable enclosed into exclamation marks will be expanded at execution time.
echo !file%filenum%!
... where %filenum% is expanded at parse time and the resulting variable is expanded at execution time
For more information about how batch line parsing works see
http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133Regards
aGerman
Re: Confusing Variables
Posted: 10 Jul 2014 08:18
by Squashman
Ugh,
I kept trying the CALL ECHO expansion but it wasn't working so I resorted to Delayed Expansion. Now I realize I was missing a percent symbol.
I had it as %%file%filenum%% which is missing a percent symbol at the end.
Re: Confusing Variables
Posted: 10 Jul 2014 08:24
by Squashman
Hmm,
Still not working. The output file name: %file1%.bat
Code: Select all
@echo off
set file1=AA-AA-AA-AA
set filenum=0
set /a filenum=%filenum%+1
call echo set %%file%filenum%%% >>%%file%filenum%%%.bat
pause
Re: Confusing Variables
Posted: 10 Jul 2014 08:37
by foxidrive
I didn't follow this thread because the question had such little detail, but is this what is wanted?
Code: Select all
@echo off
set file1=AA-AA-AA-AA
set filenum=0
set /a filenum=filenum+1
set var=%file1%%filenum%
>>%var%.bat echo %var%
pause
or this?
Code: Select all
@echo off
set file1=AA-AA-AA-AA
set filenum=0
set /a filenum=filenum+1
call set var=%%file%filenum%%%
>>%var%.bat echo %var%
pause
Re: Confusing Variables
Posted: 10 Jul 2014 08:49
by aGerman
Still not working.
You would need another CALL for the redirection which won't work of course.
As foxi showed you have to use a temporary variable.
Re: Confusing Variables
Posted: 10 Jul 2014 09:40
by RightBehindu
I have figure a way around it, sort of. Thanks for your help. I haven't tested it in the main problem yet, but Squashman sounds right with the exclamation marks. I will repost if I need some more help. Thanks Again to all.
But the original question was like so: Say I had number previously set to 1, and I have filename set to hello, and also hello1 set to true. Then I want to put those two together, and then get its variable when they are both together. It is a little confusing, but maybe something like:
Code: Select all
set final=%filename%+%number%
echo set %final%=!%final%!
where !%final%! would be: !hello1!, therefore, being true. But I am a little confused on how to do that logic out in code.