Confusing Variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RightBehindu
Posts: 26
Joined: 23 Dec 2013 07:07

Confusing Variables

#1 Post by RightBehindu » 09 Jul 2014 22:17

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

RightBehindu
Posts: 26
Joined: 23 Dec 2013 07:07

Re: Confusing Variables

#2 Post by RightBehindu » 10 Jul 2014 02:57

Just happened again. Same problem. I need to echo a specific key to a file, and I have set for example:

Code: Select all

set file1=AA-AA-AA-AA


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%

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Confusing Variables

#3 Post by Squashman » 10 Jul 2014 06:48

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

RightBehindu
Posts: 26
Joined: 23 Dec 2013 07:07

Re: Confusing Variables

#4 Post by RightBehindu » 10 Jul 2014 07:35

That is indeed what I want to do, although it wouldn't work for me. What exactly does the setlocal and EnableDelayedExpansion do?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Confusing Variables

#5 Post by Squashman » 10 Jul 2014 07:56

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Confusing Variables

#6 Post by aGerman » 10 Jul 2014 08:09

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#4095133

Regards
aGerman

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Confusing Variables

#7 Post by Squashman » 10 Jul 2014 08:18

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.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Confusing Variables

#8 Post by Squashman » 10 Jul 2014 08:24

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Confusing Variables

#9 Post by foxidrive » 10 Jul 2014 08:37

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Confusing Variables

#10 Post by aGerman » 10 Jul 2014 08:49

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.

RightBehindu
Posts: 26
Joined: 23 Dec 2013 07:07

Re: Confusing Variables

#11 Post by RightBehindu » 10 Jul 2014 09:40

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.

Post Reply